2

I followed this tutorial http://symfony.com/doc/current/forms.html and I rendered my insert form perfectly but only one problem that I can't solve:

I want to show all error of each field when the form is not valid. This is example code:

    $form->handleRequest($request);
    if (false === $form->isValid()) {
        // How to get all error message of each field ??
    }

This look like a very basic info but I can not find any guide to solve this problem. Hope someone can help me.

Thanks.

1 Answer 1

3

Accessing errors from controller

You can access errors as array using $form->getErrors() in controller.

$form->handleRequest($request);
if (false === $form->isValid()) {
    // How to get all error message of each field ??
    echo "<pre>";
    print_r($form->getErrors());
}

Accessing errors from TWIG

  • Displays all errors in template

    {{ form_errors(form) }}

  • Access error for specific field

    {{ form_errors(form.username) }}

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

2 Comments

in controller, after I getErrors, how can I get field name and it's error message?
Use $nameErrors = $form['name']->getErrors(); , More here knpuniversity.com/blog/symfony-debugging-form-errors

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.