I'm working on a symfony2 backend for a backbone.js application. I have my model and form.
我正在为backbone.js应用程序开发symfony2后端。我有自己的模特和形式。
However, backbone.js sends some additional properties to the REST API when it's creating/updating a model and I'm struggling to get the form to validate.
但是,在创建/更新模型时,backbone.js会向REST API发送一些其他属性,而我正在努力让表单进行验证。
How can I get a form in symfony2 to accept additional data, or how can I drop particular keys before binding data to a form?
如何在symfony2中获取表单以接受其他数据,或者如何在将数据绑定到表单之前删除特定键?
4 个解决方案
#1
13
To get Symfony2 accept additional data, add the additional fields to your form builder, and set their property_path
option to false:
要使Symfony2接受其他数据,请将其他字段添加到表单构建器,并将其property_path选项设置为false:
Example:
例:
$builder
->add('my_additional_field', 'checkbox', array(
'mapped' => false,
));
You don't need to drop the keys before binding the data, they'll be ignored anyway.
在绑定数据之前不需要删除密钥,无论如何它们都将被忽略。
#2
49
You should use option "allow_extra_fields".
您应该使用选项“allow_extra_fields”。
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(
array(
'allow_extra_fields' => true
)
);
}
#3
16
property_path is now deprecated in sf 2.1, use "mapped" instead
property_path现在在sf 2.1中已弃用,请改用“mapped”
$builder
->add("firstName", "text")
->add("lastName", "text")
->add("tac", "checkbox", array(
"mapped" => false
)
);
#4
2
You can listen for FormEvents::BIND_CLIENT_DATA
(or FormEvents::PRE_BIND
if you are using 2.1 dev) event listener and add/remove fields. See this cookbook entry.
您可以侦听FormEvents :: BIND_CLIENT_DATA(如果使用2.1 dev,则为FormEvents :: PRE_BIND)事件侦听器和添加/删除字段。请参阅此食谱条目。
#1
13
To get Symfony2 accept additional data, add the additional fields to your form builder, and set their property_path
option to false:
要使Symfony2接受其他数据,请将其他字段添加到表单构建器,并将其property_path选项设置为false:
Example:
例:
$builder
->add('my_additional_field', 'checkbox', array(
'mapped' => false,
));
You don't need to drop the keys before binding the data, they'll be ignored anyway.
在绑定数据之前不需要删除密钥,无论如何它们都将被忽略。
#2
49
You should use option "allow_extra_fields".
您应该使用选项“allow_extra_fields”。
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(
array(
'allow_extra_fields' => true
)
);
}
#3
16
property_path is now deprecated in sf 2.1, use "mapped" instead
property_path现在在sf 2.1中已弃用,请改用“mapped”
$builder
->add("firstName", "text")
->add("lastName", "text")
->add("tac", "checkbox", array(
"mapped" => false
)
);
#4
2
You can listen for FormEvents::BIND_CLIENT_DATA
(or FormEvents::PRE_BIND
if you are using 2.1 dev) event listener and add/remove fields. See this cookbook entry.
您可以侦听FormEvents :: BIND_CLIENT_DATA(如果使用2.1 dev,则为FormEvents :: PRE_BIND)事件侦听器和添加/删除字段。请参阅此食谱条目。