【发布时间】:2019-06-05 01:40:18
【问题描述】:
我在 Symfony 4.2.5 中使用相同的代码在加载状态和城市(estados 和 municipios)时执行 3 个表单,但只有这种表单让我抓狂,因为具有相同的代码,但它总是返回 null “municipio”,当我打印 $request 时它有 municipio=1 但在 $form->isValid() 中说 municipio 为空,它在我添加 FormEvents 时开始
如果有人可以帮助我,我将不胜感激,我检查了我所有的代码,但这就是它的中断之处。
$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()
]);
}
}
);
【问题讨论】:
-
你想用 POST_SUBMIT 事件监听器完成什么?
-
您是要访问表单填充的初始数据还是要使用用户提交的数据?
-
@Jeroen 感谢您的帮助,但我复制了我的字段,一个在 buildForm 中,另一个在事件中并导致不良行为
标签: symfony symfony-4.2