I want to show or hide the "presenceDejeuner" and "participerActivite" fields based on the selection of "presenceEvent" (set to "Oui" or "true"). Symfony waits for form validation to recognize field choices, but I need to conditionally display these fields. I've tried using an event listener, but it only works when fields are pre-filled. However, in my case, they're not. How can I achieve this, and how can I dynamically change the "required" attribute for these fields using JavaScript? I've searched extensively but haven't found a solution. Any suggestions?
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('presenceEvent', ChoiceType::class, [
'choices' => [
'Oui' => true,
'Non' => false,
],
'expanded' => false,
'multiple' => false,
])
->add('presenceDejeuner', ChoiceType::class, [
'choices' => [
'Oui' => true,
'Non' => false,
],
'expanded' => false,
'multiple' => false,
'required' => false
])
->add('participerActivite', EntityType::class, [
'mapped' => false,
'class' => Activite::class,
'choice_label' => 'activite',
'multiple' => true,
'expanded' => true,
'required' => false
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Inscription::class,
]);
}
I tryed everything what says in symfony docs such as PRE_SET_DATA , PRE_SUBMIT , Form Events etc.. Tryed also with Vanilla JS, but it just cannot recognize the choice of first field because it doesn't change dynamically in HTML
EDIT / Problem SOLVED : in Javascript, I have to find the id of the field to show or hide that was generated by symfony, I saw it in the dev tools and it was a bit strange I didn't expect that.
Change the UI
Check conditions
Listen for events