简体   繁体   English

如何在Symfony2中的特定表单元素上分配验证组

[英]How to assign a validation group on specific form element in Symfony2

I have a form class in Symfony2 and I am adding validation criteria to each element of the form (NOT to the connected entity object). 我在Symfony2中有一个表单类,并且正在向表单的每个元素添加验证条件(不向连接的实体对象添加)。 I would like to add validation groups to some of the elements but I cannot seem to figure it out how. 我想将验证组添加到某些元素中,但是似乎无法弄清楚如何进行。

I specify validation groups of the form like this: 我指定如下形式的验证组:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Acme\CommonBundle\Entity\Campaign',
        'validation_groups' => array('Default', 'new_title')
    ));
}

And on a specific form element I do this: 在特定的表单元素上,我这样做:

->add('title', 'text', array(
    'required' => true,
    'help_text' => 'This is the title.',
    'constraints' => array(new NotBlank(), new Length(array('min' => 3, 'max' => 150))),
    'validation_groups' => array('new_title')

However, this doesn't seem to be working. 但是,这似乎不起作用。 Am I doing something wrong? 难道我做错了什么?

You can assign a validation group to a specific form element ( as described in the docs for current symfony2 version ) like this: 您可以将验证组分配给特定的表单元素( 如当前symfony2版本的文档中所述 ),如下所示:

->add('title', 'text', array(
'required' => true,
'help_text' => 'This is the title.',
'constraints' => array(new NotBlank(array('groups' => array('new_title')), new Length(array('min' => 3, 'max' => 150, 'groups' => array('new_title')))),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM