Gaylord.P
Gaylord.P

Reputation: 1468

NotBlank assert and "must be of the type string, null given"

With Symfony 3.3, I have an entity With NotBlank assert :

class UserContributorVersion
{
    /**
     * @var string
     *
     * @ORM\Column(type="string")
     * @Assert\NotBlank()
     */
    private $name;

    /**
     * Set name
     * 
     * @param string $name
     */
    public function setName(string $name)
    {
        $this->name = $name;
    }
}

But If I validate form with novalidate attribute, I have this error :

Argument 1 passed to AppBundle\Entity\UserContributorVersion::setName() must be of the type string, null given

I don't understand, why force setName(string $name = null) if I have NotBlank assert ?

Thanks you :)

Upvotes: 0

Views: 2040

Answers (1)

Max P.
Max P.

Reputation: 5679

Validation is performed on form data. If form is supposed for some entity (option data_class is set) then form data is entity that should be modified. Validation is performed after request values are set to form data (in your case entity) fields.

You can find form submit flow here: https://symfony.com/doc/current/form/events.html#submitting-a-form-formevents-pre-submit-formevents-submit-and-formevents-post-submit

enter image description here

Validation is registered as FormEvents::POST_SUBMIT event. Request values are set to form data just before POST_SUBMIT event.

Upvotes: 1

Related Questions