Search code examples
symfonysymfony-formssymfony-2.6

Require a field in symfony2 server side


I have a form in Symfony2 which I am building with buildForm I add constraints like so,

$builder
->add('firstName', 'text', [
        'required' => true,
        'constraints' => [
            new NotBlank(),
        ],
    ]
)

Everything works fine until I delete the input from my html and submit it without the firstName. I don't get any errors and it submits normally. Is there a way to absolutely require the firstName, even if is not present in the submit data


Solution

  • You must use an assert with your entity as explained in the symfony documentation here

    like this:

    class User
    {
        /**
         * @orm:Column(type="string", nullable=false)
         * @assert:NotBlank
         */
        private $firstname;
    }