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条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码