Symfony 3:在表单验证中忽略了GroupSequence验证组

时间:2017-03-29 00:43:24

标签: php symfony validation

我想使用组验证来验证表单,因此我定义了两个验证组。仅当第一组尚未导致违规时,才应验证第二组。所以我这样做了:

        $form = $this->createFormBuilder(null,array('validation_groups' => new GroupSequence(array('group1','group2'))))
        ->add('email', EmailType::class,array('constraints' => array(
            new Assert\Email(array('groups' => array('group1'))),
            new Assert\NotBlank(array('groups' => array('group1'))),
            new CustomAssert\AlreadyRegistered(array('groups' => array('group2'))))))
        ->add('submit',SubmitType::class,array())
        ->getForm();

但显然没有任何约束被验证,无论我在电子邮件领域输入什么,表格都是有效的。

怎么了?

1 个答案:

答案 0 :(得分:0)

根据3.2文档,您不需要在GroupSequence下发起新的validation_groups对象。试试这个:

    $form = $this->createFormBuilder(null, array(
        'validation_groups' => array('group1','group2')
    ))
    ->add(
        'email', 
        EmailType::class,
        array(
            'constraints' => array(
                new Assert\Email(array('groups' => array('group1'))),
                new Assert\NotBlank(array('groups' => array('group1'))),
                new CustomAssert\AlreadyRegistered(array('groups' => array('group2')))
    )))
    ->add(
        'submit',
        SubmitType::class
    )
    ->getForm();

参考:http://symfony.com/doc/current/form/validation_groups.html