代码之家  ›  专栏  ›  技术社区  ›  geoB

编辑表单忽略验证批注

  •  1
  • geoB  · 技术社区  · 7 年前

    InvalidArgumentException 与消息一起抛出 Expected argument of type "string", "NULL" given . 如果从用户实体中删除验证注释,也会引发此异常。

    新表单中的同一字段为空时返回验证消息 Email address is required .

    用户实体属性(某些字段未显示):

    /**
     * @ORM\Column(type="string", length=180, unique=true)
     * @Assert\NotBlank(groups={"edit", "registration"}, message="Email address is required")
     * @Assert\Email(groups={"edit", "registration"}, message="A valid email address is required")
     */
    private $email;
    
    /**
     * @ORM\Column(type="string", length=255)
     * @Assert\NotBlank(groups={"edit", "registration"}, message="First name is required")
     */
    private $fname;
    
    /**
     * @ORM\Column(type="string", length=255)
     * @Assert\NotBlank(groups={"edit", "registration"}, message="Last name is required")
     */
    private $sname;
    

    easy_admin.yaml(某些字段未显示):

    edit:
        form_options: { validation_groups: ['edit'] }
        fields:
            - {property: 'fname', label: 'First name', type_options: { required: false }}
            - {property: 'sname', label: 'Last name', type_options: { required: false }}
            - {property: 'email', type: 'email', type_options: { required: false }}
    new:
        form_options: { validation_groups: ['registration'] }
        fields:
            - {property: 'fname', label: 'First name', type_options: { required: false }}
            - {property: 'sname', label: 'Last name', type_options: { required: false }}
            - {property: 'email', type: 'email', type_options: { required: false }}
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   famas23    7 年前

    public function setEmail(string $email): self
    {
        $this->email= $email;
    
        return $this;
    }
    

    为此:

    public function setEmail(?string $email): self
    {
        $this->email= $email;
    
        return $this;
    }