我通过AJAX更改了一些字段,但在尝试保存表单时,出现了一个错误: Extra fields are not allowed
。
如何修改类似于sf1.4中的validatorPass()
的验证器属性?
或者是否可以更改表单以接受额外的字段?
我正在使用SonataAdminBundle创建表单。
我通过AJAX更改了一些字段,但在尝试保存表单时,出现了一个错误: Extra fields are not allowed
。
如何修改类似于sf1.4中的validatorPass()
的验证器属性?
或者是否可以更改表单以接受额外的字段?
我正在使用SonataAdminBundle创建表单。
您可以在将请求数据绑定到表单之前从中删除额外的字段:
// The JSON PUT data will include all attributes in the entity, even
// those that are not updateable by the user and are not in the form.
// We need to remove these extra fields or we will get a
// "This form should not contain extra fields" Form Error
$data = $request->request->all();
$children = $form->all();
$data = array_intersect_key($data, $children);
$form->bind($data);
->add('Details', 'collection', array(
'type' => new DetailsType(),
'allow_add' => true,
'allow_delete' => true,
'label' => ' '
))
您还可以查看此问题的官方文档 http://symfony.com/doc/current/cookbook/form/form_collections.html
首先,您需要让表单集合知道它将接收未知数量的标签。到目前为止,您已添加了两个标签,表单类型期望恰好接收两个标签,否则会抛出错误:此表单不应包含额外字段。为了使其灵活,将 allow_add 选项添加到集合字段中。
由于它们未在实体中声明,因此无法添加额外的字段。 有一种解决方法可以绕过您的问题:
您可以在github上找到它的示例:https://github.com/Keirua/KeiruaProdCustomerDemoBundle
以及在此地址上的完整教程(但是是法语):
http://blog.keiruaprod.fr/2012/01/18/formulaires-dynamiques-avec-symfony2/
PS:看起来Sonata使用这种方式添加字段。