Please help me with the principle of form validation in symfony.

I create a field in the form:

public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('name', TextType::class, array( 'label' => 'Наименование' )); } 

In this case, according to the documentation, the name field will be required by default.

Now, if you go into the source code in the browser and delete required="required" , and leave the field blank, the form will certainly go off successfully, and the most interesting thing is that $form->isValid() will be true . While in essence, this field cannot be null .

 /** * @var string * * @ORM\Column(name="name", type="string", length=255) */ private $name; 

What then is the essence of form validation, if you still have to check all the fields manually? Or am I doing something wrong?

    1 answer 1

    The thing is, you have no restrictions on Entity. Verification is done by adding a set of rules (called restrictions) to the class. Here is an example:

     /** * @var string * @Assert\NotBlank() * * @ORM\Column(name="name", type="string", length=255) */ private $name; 

    The documentation is here , in the Form Validation section.

    • Not finished reading, apparently. Thank. - Pavel Sokolov
    • It does not work, I check for the type int, I enter 123 - an error from the message that I wrote, I enter 123a - This value is not valid. Those. 123 is simply not an int, but 123a is not an int? - Anton