Symfony 2.6 form validation and error messages

Steve Smith

I am fairly new to Symfony. I have written the below code to validate and return error messaged if validation failed. But I was able to only get the error message not the field which failed the validation. Below is my code:

if ($request->isXmlHttpRequest()) {

        if ($form->isValid()) {
            //do something here
        }

        $errors = $this->get('my_form')->getErrorMessages($form);
        return new JsonResponse(['errors' => $errors], 400);
}

Can someone please tell me how can I also get the field name along with the error message.

Thanks

user2268997

In order to get all the Errors of a form use $form->getErrors($deep=true, $flatten=true) so converting the errors to an array with names as field names and keys as messages will be something like:

$errors = $form->getErrors(true, true);
$errorArray = array();
foreach($errors as $e){
  //get the field that caused the error
  $field = $e->getOrigin();
  $errorArray[$field->getName()] = isset($errorArray[$field->getName()]) ? $errorArray[$field->getName()] : array();
  $errorArray[$field->getName()][] = $e->getMessage();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Symfony 2.6 form validation and error messages

From Dev

Symfony 2 form validation

From Dev

php form validation and error messages

From Dev

Symfony2 form field not updated when validation error occurs

From Dev

Symfony2 - Customized form rendering validation error

From Dev

Symfony 2 form cascading validation

From Dev

Symfony 2 form cascading validation

From Dev

Symfony2 translation of validation messages issue

From Dev

Laravel 5.0 separated error messages for form validation

From Dev

[Spring MVC - Thymeleaf]- Form validation and error messages

From Dev

Zend Form Validation set error messages

From Dev

CFWheels: Form validation customized error messages

From Dev

Codeigniter form validation error messages with bootstrap

From Dev

Form validation error messages not appearing as expected

From Dev

Validation error messages not showing in form view

From Dev

Symfony2 Form + AngularJS for REST API = Form validation extra fields error

From Dev

Obscure error on Symfony on form->handleRequest() and validation

From Dev

Datatransformer and json validation in symfony2 form

From Dev

Symfony2 form datetime fields validation

From Dev

Symfony2 conditional form validation

From Dev

Symfony2 form validation without createFormBuilder

From Dev

Symfony2 form validation exhausted memory

From Dev

Translating form validation messages

From Dev

Struts2 validation is not working properly, validation error messages not shown

From Dev

Laravel 5.1 How to output HTML in form validation error messages

From Dev

Extend contact form validation PHP script with some error messages

From Dev

Why does simple form not show validation error messages in Rails?

From Dev

Rails form validation not show the error messages, taking blank fields

From Dev

Jquery - form validation, add css styles to the error messages

Related Related

  1. 1

    Symfony 2.6 form validation and error messages

  2. 2

    Symfony 2 form validation

  3. 3

    php form validation and error messages

  4. 4

    Symfony2 form field not updated when validation error occurs

  5. 5

    Symfony2 - Customized form rendering validation error

  6. 6

    Symfony 2 form cascading validation

  7. 7

    Symfony 2 form cascading validation

  8. 8

    Symfony2 translation of validation messages issue

  9. 9

    Laravel 5.0 separated error messages for form validation

  10. 10

    [Spring MVC - Thymeleaf]- Form validation and error messages

  11. 11

    Zend Form Validation set error messages

  12. 12

    CFWheels: Form validation customized error messages

  13. 13

    Codeigniter form validation error messages with bootstrap

  14. 14

    Form validation error messages not appearing as expected

  15. 15

    Validation error messages not showing in form view

  16. 16

    Symfony2 Form + AngularJS for REST API = Form validation extra fields error

  17. 17

    Obscure error on Symfony on form->handleRequest() and validation

  18. 18

    Datatransformer and json validation in symfony2 form

  19. 19

    Symfony2 form datetime fields validation

  20. 20

    Symfony2 conditional form validation

  21. 21

    Symfony2 form validation without createFormBuilder

  22. 22

    Symfony2 form validation exhausted memory

  23. 23

    Translating form validation messages

  24. 24

    Struts2 validation is not working properly, validation error messages not shown

  25. 25

    Laravel 5.1 How to output HTML in form validation error messages

  26. 26

    Extend contact form validation PHP script with some error messages

  27. 27

    Why does simple form not show validation error messages in Rails?

  28. 28

    Rails form validation not show the error messages, taking blank fields

  29. 29

    Jquery - form validation, add css styles to the error messages

HotTag

Archive