Im doing 3 forms in Symfony 4.2.5 all with the same code when load states and cities (estados and municipios) but only this form is making me crazy because has the same code that all but doesnt work it always return null in "municipio", and when I print $request it has municipio=1 but in $form->isValid() says that municipio is null and it started when I added FormEvents
Please if someone could help me I'll be grateful, I inspected all my code but this is where its break.
$builder->get('estado')->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
$estado = $event->getForm()->getData();
if (null == $estado) {
$event->getForm()->getParent()->add('municipio', EntityType::class, [
'class' => 'App\Entity\Municipio',
'required' => true,
'placeholder' => '== Selecciona un municipio 1==',
'choices' => []
]);
} else {
$event->getForm()->getParent()->add('municipio', EntityType::class, [
'class' => 'App\Entity\Municipio',
'required' => true,
'placeholder' => '== Selecciona un municipio 2 ==',
'choices' => $estado->getMunicipios()
]);
}
}
);