Symfony validation error message for missing required form field

I have a working form, it has a required field, which should be notBlank:

/** * @Assert\NotBlank */ private $field1 = ''; 

If I specify this field in the request, but leave the field empty, I get this answer:

 { "code":400, "message":"Validation Failed", "errors":{ "children":{ "field1":{ "errors":["Field should not be blank"] } } } } 

If I omitted this field from the request, I get this response:

 { "code":400, "message":"Validation Failed", "errors":{ "errors":["Field should not be blank"] } } 

Is there some kind of built-in Symfony logic that I can use so that the second example matches the first example?

[edit] Used Symfony 2.5 - now upgraded to Symfony 2.8.3, same problem.

+5
source share
2 answers

This behavior can occur when the field is not represented in the form object itself. In this case, the image manager cannot match the validation error with one of the fields. Make sure the field is represented in the form object.

+3
source

You validate the object after processing the request. If you pass an object with an empty field 1 to the validator, this object is always invalid. This is similar to a validation error from a global level.

0
source

Source: https://habr.com/ru/post/1245505/


All Articles