5

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.

8
  • Maybe you're looking for NotNull instead NotBlank? Commented Mar 21, 2016 at 17:57
  • @walkingRed - Tried that. No dice. Commented Mar 21, 2016 at 18:01
  • What's the end goal? You need to identify which field is missing and triggering an error condition? Commented Apr 4, 2016 at 21:32
  • 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. Commented Apr 5, 2016 at 8:22
  • Have you tried $buider->add('field1', YourFormType::class, array('error_bubbling' => true)) ? Commented Apr 5, 2016 at 11:57

2 Answers 2

3
+50

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.

Sign up to request clarification or add additional context in comments.

4 Comments

The field is present in the form object, but not present in the POST data. It seems unreasonable to me that if the user omits a required field they should by default be given a completely unhelpful error message.
I can't reproduce this behavior. But if i remove the field from the form object i got the same result as you described.
So I now realise I am using the FOSBundle for form and response processing, I am doing some more analysis to find which component isn't behaving.
So I found that another dev had added an EventSubscriber that was removing fields from the form when they were omitted from the request data, in a bid to not override the defaults in the model, instead of just adding default values to the submitted data to allow validation to be applied to the default data. le sigh.
0

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.

3 Comments

If I choose not to pass a field in the request, the default value is used instead - as in a HTTP PATCH request. If the field is not valid, the form should say which field is not valid, not just leave you guessing which one is missing.
Validating over default value should be a success. Validating over empty property throws validation error on property level. Debug '$form->getErrors'. It's look likes an error on global level.
Debug '$form' object. There will be a target constraint.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.