In my Symfony formType, I have several select fields that are dynamically populated based on various user choices. One of these fields is named ‘funding’. My objective is to attach an eventListener to the ‘funding’ field so that certain actions can be triggered once this field has been populated with its options. Below is the current implementation of my FormType class. However, it appears that the eventListener for the ‘funding’ field is not being added or triggered as expected. I would appreciate any guidance or suggestions on how to resolve this issue.
class UserRegisterSessionAdminType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$this->role = $options['role'];
$builder->add('formation' , EntityType::class, [
'mapped' => false,
'class' => Formation::class,
'choice_label' => 'name',
'placeholder' => 'Choisir une formation',
'required' => false,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('f')
->join('f.sessions', 's') // Assuming that the Formation entity has a property named "session" that maps to the Session entity
->where('s.id IS NOT NULL'); // Check that the session id is not null
},
])
->add('session', ChoiceType::class, [
'placeholder' => 'Choisir une session'
])
;
if($this->role == 'candidate') {
$builder->add('funding', ChoiceType::class, [
'placeholder' => 'Choisir un financement'
]);
};
$formModifier = function (FormInterface $form, Formation $formation = null) {
$sessions = null === $formation ? [] : $formation->getSessions();
$fundings = $this->formationFundingRepo->findByFormation($formation);
$fundingEntities = array_map(function($formationFunding) {
return $formationFunding->getFunding();
}, $fundings);
$form->add('session', EntityType::class, [
'class' => Session::class,
'choices' => $sessions,
'required' => false,
'choice_label' => function (Session $session) {
return 'Du '.$session->getBeginsAt()->format('d-m-Y')." au ".$session->getEndsAt()->format('d-m-Y');
},
'placeholder' => 'Select a session'
]);
if($this->role == 'candidate') {
$form->add('funding', EntityType::class, [
'class' => Funding::class,
'choices' => $fundingEntities,
'required' => false,
'placeholder' => 'Choisir un financement'
]);
}
};
$builder->get('formation')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($formModifier) {
$formation = $event->getForm()->getData();
$this->logger->info('Fromation id: ' . $formation);
$formModifier($event->getForm()->getParent(), $formation);
}
);
// Add an event listener to the funding field to add/remove the employer field based on isCoordEmployer
$builder->get('funding')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) {
$this->logger->info('event listener');
$form = $event->getForm();
$funding = $form->getData();
dd('triggered');die;
if ($funding && $funding->isIsCoordEmployer()) {
$this->logger->info('added');
$form->getParent()->add('employer', EmployerType::class);
} else {
$form->getParent()->remove('employer');
$this->logger->info('remove');
}
}
);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => UserSession::class,
'role' => 'candidate'
]);
}
}