在简单的用户管理实现中(Symfony 4.1.7,easyadmin 1.17.16),编辑表单将忽略实体验证注释,而新表单则不会。例如,如果提交表单时编辑表单中的电子邮件字段为空,则会将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 }}
答案 0 :(得分:1)
这是一个参数异常。尝试将您的setters参数更改为接受null参数。这应该可以正常工作,并且会触发验证回调,例如:
public function setEmail(string $email): self
{
$this->email= $email;
return $this;
}
对此:
public function setEmail(?string $email): self
{
$this->email= $email;
return $this;
}