Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 3.4 - form_errors form theme not displaying

EDIT: Doing some digging, and it looks like my problem stems from the fact that the form I'm creating builds off of the FOSUserBundle's registration form. Why is that an issue? Because while their RegistrationController throws a REGISTRATION_FAILURE event if the form isn't valid, it doesn't look like the event has a listener. So, the invalid state doesn't actually do anything aside from not being handled at all by the system. It doesn't even return the form (with errors).

I have a relatively simple form theme I want to use in order to display my form errors in a block at the top of my form:

{% form_theme form _self %}

{% block form_errors %}
    {% spaceless %}
        {% if errors|length > 0 %}
            <div class="errors pt-2 mb-2">
                <ul>
                    {% for error in errors %}
                        <li>{{ error.message }}</li>
                    {% endfor %}
                </ul>
            </div>
        {% endif %}
    {% endspaceless %}
{% endblock form_errors %}

I reference it in the form template with:

{% form_theme form 'Form/errors.html.twig' %}

And then I try to invoke it with a simple:

{{ form_errors(form) }}

But, when I intentionally provide incorrect data to my form inputs and try to submit, there's no error div present. The submission fails, due to there being 6 validation errors, but no errors are displayed/in the HTML source.

Looking at the documentation, I think I'm doing it right, but I'm obviously missing something.

like image 483
Major Productions Avatar asked Nov 26 '22 00:11

Major Productions


1 Answers

You have two problems and you have to solve both of them in order to make the form validation work.

1st Problem:

First problem is that the listener is not working. If you intentionally make errors in the form data and the form validation is not kicking in, then you will never see the form_errors template. You have to go over the documentation for the FOSUserBundle. First, check if you enabled/configured the validation.yml correctly. Secondly, check the PHP class that generates the form. Make sure that the validation criteria are set properly. Now, the most reliable way to check if the form is producing errors is to {{dump(form)}} in the Twig template. It will show all the data for the form, including the errors. Keep working at it until you manage to see some errors. Otherwise you will not be able to do the next step.

2nd Problem:

The second problem is that you probably not including the correct template. I can not count how many times I had the same problem. You may have 3 or 4 templates that contain html/twig code for <form> but Symfony actually uses like 1 or 2 of them. Add your code to every one of them and make sure that your Twig block is not overridden by some other form block.

Also keep in mind that you have to clean both the app cache (php bin/console cache:clean -e prod) and the browser cache after every change in the templates. You might have done everything right and the damn cache is showing the cached results. One last advice: use the browser developer tools. Often you find a lot of information and errors are shown in the tools' console.

like image 59
John Krit Avatar answered Dec 06 '22 01:12

John Krit