我正在尝试创建一个动态表单,就像Symfony的示例of dynamic form modification
我有一个父表单类型“AttributeType”,其中包含“AttributeDetailTypes”的集合。子AttributeDetailType()表单是我设置监听器的地方:
$attributeDetailFields = $options['attributeDetailFields'];
$builder
->add('attributeDetailField', 'entity', array(
'class' => 'RelayRiseBundle:AttributeDetailField',
'choices' => $attributeDetailFields,
'property' => 'name'
));
我的听众将使用基于用户选择的'attributeDetailField'的字段类型填充字段'value':
$formModifier = function (FormInterface $form, AttributeDetailField $attributeDetailField = null) {
print($attributeDetailField->getName());
if($attributeDetailField->getDataType()->getValue() === 'date') {
$form->add('value', 'collot_datetime', array('pickerOptions' =>
array("format" => "mm/dd/yyyy",
"todayBtn" => "linked",
"autoclose" => true,
"minView" => "month",
"todayHighlight" => true),
));
} else {
$form->add('value', 'text');
}
}
所以我有PRE_SET_DATA和POST_SUBMIT事件监听器。
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier) {
if (null != $event->getData()) {
$data = $event->getData();
$formModifier($event->getForm(), $data->getAttributeDetailField());
}
}
);
$builder->get('attributeDetailField')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($formModifier) {
if (null != $event->getData()) {
$attributeDetailField = $event->getForm()->getData();
$formModifier($event->getForm()->getParent(), $attributeDetailField);
}
}
);
填充表单时,PRE_FORM_DATA侦听器工作正常。问题是当我对控制器进行AJAX回调时触发表单>提交。然后调用POST_SUBMIT侦听器和PRE_FORM_DATA侦听器,我收到以下错误:
“检测到一个循环。对PRE_SET_DATA事件的监听器不能调用setData()。你应该在FormEvent对象上调用setData()而不是”
如何通过Ajax提交表单,触发我的POST_SUBMIT监听器而不抛出PRE_SET_DATA错误?