Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony Validation Error Message for Missing Required Form Field

I have a working Form, it has a required field that needs to be notBlank:

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

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

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

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

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

Is there some built-in Symfony logic somewhere that I can use to make the second example match the first example?

[edit] Was using Symfony 2.5 - now updated to Symfony 2.8.3, same problem.

like image 455
StampyCode Avatar asked Oct 19 '22 13:10

StampyCode


1 Answers

This behavior can occur when the field is not presented in the form object itself. In this case violation mapper can't map validation error to one of the fields. Please check that the field is presented in the form object.

like image 155
Sergey Fedotov Avatar answered Oct 21 '22 06:10

Sergey Fedotov