Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Form errors rendered twice for one field. #29187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jawadmjn opened this issue Nov 12, 2018 · 9 comments
Closed

Form errors rendered twice for one field. #29187

jawadmjn opened this issue Nov 12, 2018 · 9 comments

Comments

@jawadmjn
Copy link

jawadmjn commented Nov 12, 2018

Symfony version(s) affected: 4.1

Description
It occurs in this commit

By putting errors in the label now the following issue occurs. And we can not use Customize Form Rendering

For example when I am putting a field like this:

{{ form_label(attraction.surroundings) }}

{{ form_widget(attraction.surroundings) }}

{{ form_help(attraction.surroundings) }}

{{ form_errors(attraction.surroundings) }}

And Error occured then The error message is one time showing in label and 2nd time what i put form_errors.

screen shot 2018-11-12 at 13 57 11

Possible Solution
So we should take errors out from the label and have to put form_errors in form_row And we have to put form_errors in the end otherwise if the error occurs then error area will push the whole layout down.

{% block form_row -%}
    {%- if compound is defined and compound -%}
        {%- set element = 'fieldset' -%}
    {%- endif -%}
    {%- set widget_attr = {} -%}
    {%- if help is not empty -%}
        {%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
    {%- endif -%}
    <{{ element|default('div') }} class="form-group">
        {{- form_label(form) -}}
        {{- form_widget(form, widget_attr) -}}
        {{- form_help(form) -}}
        **{{- form_errors(form) -}}**
    </{{ element|default('div') }}>
{%- endblock form_row %}
@ro0NL
Copy link
Contributor

ro0NL commented Nov 12, 2018

Would be solved if you don't render {{ form_errors(attraction.surroundings) }} yourself right?

Otherwise, why cant you disable the errors in the label by overriding its block: {% block form_label_errors %}

{% form_theme form _self %}

{% block form_label_errors %}
{% endblock %}

{{ your form }}

no?

@stof
Copy link
Member

stof commented Nov 12, 2018

The Bootstrap 4 theme renders errors inside form_label, for accessibility reasons. Rendering them manually outside it is the reason why you have them rendered twice.

@jawadmjn
Copy link
Author

jawadmjn commented Nov 12, 2018

If we can not render error on our own then this thing is against this document Customize Form Rendering

And if I am just using
{{ form_row(attraction.surroundings) }}

Then out looks like this:
Error rendering in label and pushing widget down.

screen shot 2018-11-12 at 15 15 29

@stof
Copy link
Member

stof commented Nov 12, 2018

the page you link has a note about reading more on the BS 4 theme, and that page tells you about the fact that errors are rendered inside the label: https://symfony.com/doc/current/form/bootstrap4.html#labels-and-errors

@javiereguiluz would it make sense to add a dedicated note in the Customize Form Rendering page about that ?

@jawadmjn
Copy link
Author

@stof I am perfectly fine what you said but the thing is that the page layout is broken as you can see in the image I attached.

@xabbuh
Copy link
Member

xabbuh commented Feb 2, 2019

I am closing here as this is the expected behaviour (though currently being discussed in #28086).

@xabbuh xabbuh closed this as completed Feb 2, 2019
@zangab
Copy link

zangab commented May 26, 2020

guess i'm late but @jawadmjn is right @xabbuh

this does not work as expected. as it's written in the docs (https://symfony.com/doc/current/form/form_customization.html) the form_errors(...) should display the input fields' errors. BUT as it is right now, the whole form_errors(...) are getting appended in the form_label(...) and this does not match with the illustrated diagram in the docs. the form_label(...) responsibility should be to render the label only.

the provided code sample in the docs would be also wrong as it ouputs the errors twice:

<div class="form-control">
    <i class="fa fa-calendar"></i> {{ form_label(form.dueDate) }}
    {{ form_widget(form.dueDate) }}

    <small>{{ form_help(form.dueDate) }}</small>

    <div class="form-error">
        {{ form_errors(form.dueDate) }}
    </div>
</div>```

@xabbuh
Copy link
Member

xabbuh commented May 26, 2020

It looks like symfony/symfony-docs#13685 will clarify that.

@ThomasLandauer
Copy link
Contributor

Now online at https://symfony.com/doc/4.4/form/form_customization.html#form-errors-form-view

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants