-1

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

user3410843
  • 195
  • 3
  • 18

2 Answers2

1

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;
}
Benoît
  • 308
  • 1
  • 10
  • When making different asserts it is only checked when the input is in the submitted data field. I want to require the form to contain a certain field to be present in the submit data. After that it will validate normally with asserts. – user3410843 Feb 21 '18 at 10:57
  • don't forget to indicate "nullable=false" in the properties of your entity's $firstname field. – Benoît Feb 21 '18 at 11:13
0

You did not submit any data, the form is not submitted hence no validation is triggered.

Instead of:

$this->handleRequest($request);

Try to always submit the form even if the data is missing:

$form->submit($request->request->all());

I cannot guarantee this code is valid in your context since you did not provide your controller code.

Mike Doe
  • 16,349
  • 11
  • 65
  • 88