0
votes

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 :)

1
because if you just pressed the submit button without inserting any data, then the fields are NULL, and you typehinted that you expect a string - Dan Costinel

1 Answers

1
votes

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.