该文档说明了how to dynamically modify forms using form events,尤其是POST_SUBMIT事件。 $form->add('position', 'text', array('data' => 'It works'));
预填充动态表单字段
该字段添加,但为空:
根据这个实例,在POST_SUBMIT事件监听器,我不能像预填充数据添加一个字段。
任何想法我该怎么做?
编辑
基本上表单类型应该是这样的:
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
));
});
}
你可以添加更多的信息吗?就像事件监听器类或表单类型(如果第一个不存在)。 – cheesemacfly
我的猜测是表单正在尝试使用提交的数据来填充字段,而没有提交数据。 – Andrew