5
votes

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.

2
Maybe you're looking for NotNull instead NotBlank?walkingRed
@walkingRed - Tried that. No dice.StampyCode
What's the end goal? You need to identify which field is missing and triggering an error condition?miken32
The end goal is I want to know why the default behaviour is to attribute generic "field missing" errors at the form level, smells like a bug.StampyCode
Have you tried $buider->add('field1', YourFormType::class, array('error_bubbling' => true)) ?Heah

2 Answers

3
votes

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.

0
votes

You are validating an entity after handling the request. If you pass the entity with empty 'field1' to the validator this object is always not valid. It's looks like a validation error from global level.