简体   繁体   English

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

[英]Symfony 3: GroupSequence validation groups ignored in validation of form

I would like to validate a form using group validation and therefore I defined two validation groups. 我想使用组验证来验证表单,因此我定义了两个验证组。 The second group should only be validated if the first group does not already cause violations. 仅当第一组尚未导致违规时,才应验证第二组。 So I did this: 所以我这样做了:

        $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();

But apparently none of the constraints are validated, the form is valid no matter what I enter in the email field. 但显然没有任何约束被验证,无论我在电子邮件字段中输入什么,表单都是有效的。

What's wrong? 怎么了?

According to the 3.2 documentation you don't need to initiate a new GroupSequence object under the validation_groups . 根据3.2文档,您不需要在validation_groups下启动新的GroupSequence对象。 Try this: 尝试这个:

    $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();

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

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

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