dongyi6269 2014-01-06 04:13
浏览 60
已采纳

symfony组验证如何工作

I am little bit confused regarding symfony group validation.

Suppose i have this code

 * @NotBlank(groups={"A", "B", "C"})
 */
private $description;

When i submit the form then i am manually inject groups like this

$this->validator->validate($object, groups={"F", "A","C"})

Now i want to know how symfony will do validation

  1. Does symfony check that all the groups ie, F,A,C SHOULD MATCHED WITH A,B,C OR it checks if any groups from F,A,C is present in defined gorup'A,B,C'. So if any item macthes then it do the validation
  • 写回答

2条回答 默认 最新

  • dtkwt62022 2014-01-06 09:16
    关注

    If you take a look at the validate() method signature, you may understand that you should not think of the $groups parameter it as a parameter that allows you to inject validation groups.

    It's used to ask your validator to validate a given object against some groups of constraints.

    Example of use,

    /*
     * @NotBlank(groups={"A", "B"})
     */
    private $property1;
    
    /*
     * @NotBlank(groups={"C"})
     */
    private $property2;
    
    /*
     * @NotBlank(groups={"B"})
     */
    private $property3;
    

    Then,

    $this->validator->validate($object, groups={"A", "C"})
    

    will validate your property1 & property2 against the NotBlank constraint.

    But when calling,

    $this->validator->validate($object, groups={"A", "B"})
    

    only property1 & property3 are validated againt the NotBlank constraint as group C isn't invoked.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?