编辑表单忽略验证注释

Edit form ignores validation annotations

在简单的用户管理实现中(Symfony 4.1.7,easyadmin 1.17.16),编辑表单忽略实体验证注释,而新表单则不会。例如,如果在提交表单时编辑表单中的电子邮件字段为空,则会抛出 InvalidArgumentException 和消息 Expected argument of type "string", "NULL" given。如果从用户实体中删除验证注释,也会抛出此异常。

新表单中为空时的相同字段returns验证消息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 }}

这是一个参数异常。尝试更改您的 setters 参数以接受 null 参数。这应该有效并会触发验证回调,例如:

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

    return $this;
}

为此:

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

    return $this;
}