本文介绍了symfony2 在表单中使用验证组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有属性的实体:
I have a Entity with a property:
/**
* @var string $name
*
* @AssertNotBlank(groups={"foobar"})
* @ORMColumn(name="name", type="string", length=225, nullable=false)
*/
private $name;
表格:
class MyType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('name');
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => '...',
'validation_group' => array('foobar'),
);
}
public function getName()
{
...
}
}
在控制器中我绑定请求并调用 $form->isValid()
In the Controller I bind the Request and call $form->isValid()
但是如何定义validation_group?
But how to define the validation_group?
推荐答案
在控制器中构建表单时,在选项数组中添加一个 'validation_groups' 项:
When building the form in the controller, add a 'validation_groups' item to the options array:
$form = $this->createFormBuilder($users, array(
'validation_groups' => array('foobar'),
))->add(...)
;
它在symfony2书的表单页面中有描述:http://symfony.com/doc/current/book/forms.html#validation-groups
It is described in the forms page of the symfony2 book: http://symfony.com/doc/current/book/forms.html#validation-groups
这篇关于symfony2 在表单中使用验证组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!