【发布时间】:2012-01-31 11:29:49
【问题描述】:
我通过 AJAX 更改了一些字段,当我尝试保存表单时,我收到一个错误 Extra fields are not allowed。
如何在 sf1.4 中更改像 validatorPass() 这样的验证器属性?
或者它可能会更改表单以接受额外的字段?
我正在使用 SonataAdminBundle 创建表单。
【问题讨论】:
标签: php forms symfony sonata-admin
我通过 AJAX 更改了一些字段,当我尝试保存表单时,我收到一个错误 Extra fields are not allowed。
如何在 sf1.4 中更改像 validatorPass() 这样的验证器属性?
或者它可能会更改表单以接受额外的字段?
我正在使用 SonataAdminBundle 创建表单。
【问题讨论】:
标签: php forms symfony sonata-admin
您可以在将请求数据绑定到表单之前从请求数据中删除额外的字段:
// 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/formulaires-dynamiques-avec-symfony2/
PS:看来奏鸣曲就是用这种方式添加字段的。
【讨论】: