Search code examples
phpsymfonyeasyadmin

Edit form ignores validation annotations


In a simple user admin implementation (Symfony 4.1.7, easyadmin 1.17.16) the edit form ignores entity validation annotations whereas the new form does not. For example, if an email field in the edit form is empty when the form is submitted an InvalidArgumentException is thrown with the message Expected argument of type "string", "NULL" given. This exception is also thrown if the validation annotation is removed from the User entity.

The same field when empty in the new form returns the validation message Email address is required.

User entity properties (some fields not shown):

/**
 * @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 (some fields not shown):

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 }}

Solution

  • It's an argument exception. Try to change your setters argument to accept null argument. This should work and gonna hit the validation callback, for example:

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

    To this:

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