【问题标题】:Symfony 3 group validationSymfony 3 组验证
【发布时间】:2016-04-21 18:35:39
【问题描述】:

它没有像我预期的那样工作。

简单验证.yml

ForumBundle\Entity\User:
properties:
    login:
        - NotIdenticalTo:
            value: ROOT
            message: Some Error
    phone:
        - NotBlank:
            message: Phone must be declared!
            groups: [admin]
    granted_by:
        - NotBlank:
            message: Your promotor must be declared!
            groups: [admin]

我想有两种方法:

如果用户不是管理员,只需验证登录属性(工作正常)

如果用户是管理员,则使用 admin 组和登录属性验证所有属性。

我尝试添加这样的代码:

        $errors = $this
        ->get('validator')
        ->validate($user, null, array('admin'));

但是当用户是管理员时,它只是使用管理员组验证属性(登录字段未验证)。有没有不添加更多组的解决方案?

【问题讨论】:

    标签: php validation symfony


    【解决方案1】:

    确保login 属性的约束是Defaultadmin 组的一部分:

    ForumBundle\Entity\User:
        properties:
            login:
                - NotIdenticalTo:
                    value: ROOT
                    message: Some Error
                    groups: [Default, admin]
            phone:
                - NotBlank:
                    message: Phone must be declared!
                    groups: [admin]
            granted_by:
                - NotBlank:
                    message: Your promotor must be declared!
                    groups: [admin]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 2017-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-28
      • 1970-01-01
      相关资源
      最近更新 更多