我通过 AJAX 更改了一些字段,当我尝试保存表单时,我收到一个错误Extra fields are not allowed
。
validatorPass()
如何像sf1.4那样更改验证器属性?
或者它可能改变形式以接受额外的字段?
我使用 SonataAdminBundle 创建表单。
我通过 AJAX 更改了一些字段,当我尝试保存表单时,我收到一个错误Extra fields are not allowed
。
validatorPass()
如何像sf1.4那样更改验证器属性?
或者它可能改变形式以接受额外的字段?
我使用 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);
在我的情况下,解决方案非常简单,只需将 allow_add 添加到您的集合字段中,在我的示例下方
->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/formulares-dynamiques-avec-symfony2/
PS:看来奏鸣曲是用这种方式添加字段的。