symfony2在表单中使用验证组 [英] symfony2 using validation groups in form

查看:133
本文介绍了symfony2在表单中使用验证组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体有一个属性:

I have a Entity with a property:

/**
 * @var string $name
 *
 * @Assert\NotBlank(groups={"foobar"})
 * @ORM\Column(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()
    {
        ...
    }
}

在Controller中绑定请求并调用$ 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在表单中使用验证组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆