在一个简单的用户管理实现(Symfony 4.1.7,easyadmin 1.17.16)中,编辑表单会忽略实体验证注释,而新表单则不会。例如,如果在提交表单时编辑表单中的电子邮件字段为空,InvalidArgumentException
则会与 message 一起抛出Expected argument of type "string", "NULL" given
。如果从 User 实体中删除了验证注释,也会引发此异常。
新表单中的相同字段为空时返回验证消息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 }}