I have 3 fields, one of them is generated when the creation of the form is invoked. But the problem is that each of the following fields are dependent with their children. The idea is because by javascript, I update these fields.
Example: country -> state -> city
The problem I have is the following, and it is that the City values do not reach me, once the form is submitted. I only get the Country and State, and the form is all the time indicating that the correct values have not been entered for that reason. I think the solution would be to include an AddEventListenes POST_SUBMIT when the State field is added, but I can't think how I can do it.
class UserAddressFormType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { // Build FORM $builder ->add('address', TextType::class, ['label' => 'Dirección','required' => true,'attr' => ['placeholder' => 'Dirección','class' => 'form-control user_form_name'], ]) ->add('country', EntityType::class, ['label' => 'País','class' => Country::class,'choice_value' => function (?Country $entity) { return $entity ? $entity->getISOCountry() : ''; },'required' => true,'choice_label' => 'countryName','placeholder' => 'Elige un país..','attr' => ['placeholder' => 'País','class' => 'form-control select2 user_form_name'], ]) ->add('postalCode', TextType::class, ['label' => 'Código Postal','required' => true,'attr' => ['placeholder' => 'Código Postal','class' => 'form-control user_form_phone'], ]) ->add('send', SubmitType::class, ['label' => 'Actualizar','attr' => ['class' => 'btn user_form_send' ], ]); $builder->addEventListener( FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($builder) { /** @var UserAddress|null $data * */ $data = $event->getData(); if (!$data) { return; } $this->setupSpecificStateField( $event->getForm(), $data->getCountry() ); $this->setupSpecificCityField( $event->getForm(), $data->getState() ); }); $builder->get('country')->addEventListener( FormEvents::POST_SUBMIT, function(FormEvent $event) { $form = $event->getForm(); $this->setupSpecificStateField( $form->getParent(), $form->getData() ); }); } private function setupSpecificStateField(FormInterface $form, ?Country $country) { $ISOCountry = ''; if ($country != null) { $ISOCountry = $country->getISOCountry(); } // Create NEW SELECT $form->add('state', EntityType::class, ['label' => 'Provincia/Estado','class' => State::class,'query_builder' => function (EntityRepository $er) use ($ISOCountry) { return $er->createQueryBuilder('e') ->where('e.ISOCountry = :country') ->setParameter('country', $ISOCountry) ->orderBy('e.nameState', 'ASC'); },'choice_value' => function (?State $entity) { return $entity ? $entity->getISOState() : ''; },'required' => true,'choice_label' => 'nameState','placeholder' => 'Elige una provincia..','attr' => ['placeholder' => 'Provincia/Estado','class' => 'form-control select2 user_form_surname'], ]); } private function setupSpecificCityField(FormInterface $form, ?State $state) { $ISOState = ''; if ($state != null) { $ISOState = $state->getISOState(); } // Create NEW SELECT $form->add('city', EntityType::class, ['label' => 'Ciudad/Población','class' => City::class,'query_builder' => function (EntityRepository $er) use ($ISOState) { return $er->createQueryBuilder('c') ->where('c.ISOState = :state') ->setParameter('state', $ISOState) ->orderBy('c.nameCity', 'ASC'); },'choice_value' => function (?City $entity) { return $entity ? $entity->getNameCity() : ''; },'required' => true,'choice_label' => 'nameCity','placeholder' => 'Elige una ciudad..','attr' => ['placeholder' => 'Ciudad/Población','class' => 'form-control select2 user_form_email'], ]); } public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(['data_class' => UserAddress::class, ]); }}
I think the solution would be to add something like the following in the following function, but it doesn't work because addEventListener is not accessible.
private function setupSpecificStateField(FormInterface $form, ?Country $country) { $ISOCountry = ''; if ($country != null) { $ISOCountry = $country->getISOCountry(); } // Create NEW SELECT $form->add('state', EntityType::class, ['label' => 'Provincia/Estado','class' => State::class,'query_builder' => function (EntityRepository $er) use ($ISOCountry) { return $er->createQueryBuilder('e') ->where('e.ISOCountry = :country') ->setParameter('country', $ISOCountry) ->orderBy('e.nameState', 'ASC'); },'choice_value' => function (?State $entity) { return $entity ? $entity->getISOState() : ''; },'required' => true,'choice_label' => 'nameState','placeholder' => 'Elige una provincia..','attr' => ['placeholder' => 'Provincia/Estado','class' => 'form-control select2 user_form_surname'], ]); $form->get('state')->addEventListener( FormEvents::POST_SUBMIT, function(FormEvent $event) { $form = $event->getForm(); $this->setupSpecificCityField( $form->getParent(), $form->getData() ); }); }
I would like to see if anyone has any idea how to solve this, because I am going crazy. I have searched, but I find that no one has had this same case, anywhere.
Greetings and thanks.