该文档解释了如何使用表单事件动态修改表单,尤其是 POST_SUBMIT 事件。
按照此示例,在 POST_SUBMIT 事件侦听器中,我无法添加具有预填充数据的字段,例如:$form->add('position', 'text', array('data' => 'It works'));
该字段已添加但为空。
知道我该怎么做吗?
编辑
基本上表单类型如下所示:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('colors', 'choice', array(
'choices' => array('blue', 'green', 'red'),
'multiple' => true,
'expanded' => true,
'mapped' => false,
));
$builder->get('colors')->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
// ... Some logic to determine data to pre populate $myValue
$myValue = 'It works';
$form = $event->getForm()->getParent();
$form->add('position', 'text', array(
'data' => $myValue
));
});
}