0

As Symfony2 Form component with tight integration with the validation component is extremely powerful, I have hard times understanding how I can get the list of validation errors at PHP side with proper reference to the invalid field. By "reference", I mean some hint that I can further get the field and it's ID (like with $form->get($failedChild)->vars['id'] or something).

$form->getErrors() returns the list of the errors, but this array of FormErrors does not include any reference to the field that is invalid.

I found a Gist that shows something like that, but at least on 2.3 or 2.4 all errors are still bound to the main form.

error_bubbling option does not change the behavior as I first thought it would.


Update #1

In the end, I'd like to receive a JSON representation of all form errors and return it back to the client. A sample JSON would be something like

{
    "invalid_field_id": "This field has invalid value"
}

The ID of the field can be received over $field->vars['id'] but it seems that all validation errors are returned in $form->getErrors() instead of $field->getErrors()

1 Answer 1

1

You can use $form->getErrorsAsString() to get a human-readable representation of your errors. These errors are mapped to fields, unless the error can not be attached to a field, or error_bubbling is set to true.

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

1 Comment

Thanks. This is not exactly what I am looking for, I added update to the question to further clarify what are my intentions. ;)

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.