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

Skip to content

[Bootrap 4 Theme] Form validation errors do not match official Bootstrap documentation #28086

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
frostieDE opened this issue Jul 29, 2018 · 23 comments

Comments

@frostieDE
Copy link

frostieDE commented Jul 29, 2018

Symfony version(s) affected: (at least) 4.1.2

Description
When enabling the Bootrap 4 form theme in the latest version of Symfony, validation errors are not displayed correctly*:

The error is displayed underneath the label instead of the actual widget. Fix: move the corresponding block to the correct position.

image

*) "correctly" means that is looks different from the original documentation here

How to reproduce

  1. Configure twig to use the Bootrap 4 theme
  2. Create a form and cause a validation error

Possible Solution
As suggested above.

@frostieDE
Copy link
Author

In addition, this is the expected layout of validation errors:

image

@xabbuh
Copy link
Member

xabbuh commented Aug 3, 2018

Can you confirm that #27892 will fix your issue?

@frostieDE
Copy link
Author

@xabbuh Yes, this should fix my issue 😉

@FlashBlack
Copy link

Came from #29986 , it needs to be done.

Read the arguments from this closed topic (#29986).
I also propose to abandon the badge "Error", it is not in official bootstrap style, or expand with the possibility of changing it via block "form_error_badge"

@Nyholm
Copy link
Member

Nyholm commented Apr 6, 2019

Thank you for this issue. I have reviewed this and discussed it many times but the solution is more tricky that you first expect.

I do agree that there is an issue. However the implementation is intentional. We really want the error messages in the labels because that helps screen readers and blind users.

At #eu-fossa we were a bunch of people that sat done and discussed this to find a solution. We came up with the following solution:

  • We introduce an option for twig context like errors_in_labels that are default true
  • bootstrap_4_layout.html.twig has the errors in the labels as default
  • bootstrap_4_horizontal_layout.html.twig has errors_in_labels: false
  • When ever errors_in_labels: false we add the error message under the input field as Bootstrap suggests. We should also use aria-describedby (See [docs](https://www.w3.org/WAI/tutorials/forms/notifications/#after-submit
<div class="error">
	<label for="username">
		<strong>Error:</strong> <!-- Maybe we should add line this too? -->
		Username:
	</label>
	<input type="text" 
                name="username"
		id="username"
		aria-describedby="username-desc">
	<span id="username-desc">The username is already taken.</span>
</div>

@frostieDE
Copy link
Author

Sounds like a great solution to me :-) Is there an ETA for the implementation of this solution?

@Nyholm
Copy link
Member

Nyholm commented Apr 6, 2019

As far as I know. Nobody is correctly working on it. Feel free to write a PR. If you do, please make a comment here.

@frostieDE
Copy link
Author

I will give it a try next week :-) But I have one question regarding the errors_in_labels option. How will this option be set? Will this option be made available as configuration option of the twig bridge component? Or is there another way of setting such option?

@pierrejolly
Copy link

pierrejolly commented May 3, 2019

Tell me if I wrong but IMO it's not to us to defined how should be rendered errors with Bootstrap (inside or outside label for accessibility purposes).

Bootstrap documentation already specify how errors should be rendered, if we implement a Bootstrap form theme we must follow Bootstrap documentation and not make our own opinionated layout.

But maybe I miss something :).

@vladimir-light
Copy link

any updates ?

@frostieDE
Copy link
Author

Not yet as I did not have any time to work on this.

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot
Copy link

Hello? This issue is about to be closed if nobody replies.

@frostieDE
Copy link
Author

I am using the following snippet as a workaround (unfortunately I do not know where I found this snippet, so credets for whoever wrote this):

{% block form_row -%}
    <div class="form-group row{% if (not compound or force_error|default(false)) and not valid %} is-invalid{% endif %}">
        {{- form_label(form) -}}
        <div class="{{ block('form_group_class') }}">
            {{- form_widget(form) -}}
            {{- form_errors(form) -}}
            {{- form_help(form) -}}
        </div>
    </div>
{%- endblock form_row %}

@carsonbot carsonbot removed the Stalled label Mar 4, 2021
@DoobleD
Copy link

DoobleD commented Mar 14, 2021

Here's my workaround for Symfony 4.4. The code is taken from bootstrap_4_layout.html.twig, I commented the extra errors stuff in form_label and form_errors, and added a form_errors in form_row, as did @frostieDE. Probably not great, looking forward a real fix.

{% extends "bootstrap_4_layout.html.twig" %}

{% block form_label -%}
    {% if label is not same as(false) -%}
        {%- if compound is defined and compound -%}
            {%- set element = 'legend' -%}
            {%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' col-form-label')|trim}) -%}
        {%- else -%}
            {%- set label_attr = label_attr|merge({for: id}) -%}
        {%- endif -%}
        {% if required -%}
            {% set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' required')|trim}) %}
        {%- endif -%}
        {% if label is empty -%}
            {%- if label_format is not empty -%}
                {% set label = label_format|replace({
                    '%name%': name,
                    '%id%': id,
                }) %}
            {%- else -%}
                {% set label = name|humanize %}
            {%- endif -%}
        {%- endif -%}
        <{{ element|default('label') }}{% if label_attr %}{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}{% endif %}>
            {{ translation_domain is same as(false) ? label : label|trans(label_translation_parameters, translation_domain) }}
            {# {% block form_label_errors %}{{- form_errors(form) -}}{% endblock form_label_errors %} #}
        </{{ element|default('label') }}>
    {%- else -%}
        {# {%- if errors|length > 0 -%}
        <div id="{{ id }}_errors" class="mb-2">
            {{- form_errors(form) -}}
        </div>
        {%- endif -%} #}
    {%- endif -%}
{%- endblock form_label %}

{% 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') }}{% with {attr: row_attr|merge({class: (row_attr.class|default('') ~ ' form-group')|trim})} %}{{ block('attributes') }}{% endwith %}>
        {{- form_label(form) -}}
        {{- form_widget(form, widget_attr) -}}
        {{- form_errors(form) -}}
        {{- form_help(form) -}}
    </{{ element|default('div') }}>
{%- endblock form_row %}

{% block form_errors -%}
    {%- if errors|length > 0 -%}
        <span class="{% if form is not rootform %}invalid-feedback{% else %}alert alert-danger{% endif %} d-block">
            {%- for error in errors -%}
                <span class="d-block">
                    {# <span class="form-error-icon badge badge-danger text-uppercase">{{ 'Error'|trans({}, 'validators') }}</span> #}
                    <span class="form-error-message">{{ error.message }}</span>
                </span>
            {%- endfor -%}
        </span>
    {%- endif %}
{%- endblock form_errors %}

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@DoobleD
Copy link

DoobleD commented Sep 15, 2021

On my end: I'm still relying on the workaround I mentioned above.

@carsonbot carsonbot removed the Stalled label Sep 15, 2021
@FlashBlack
Copy link

I resigned myself and just accept that the implementation difference should be in your own cookbook

@frostieDE
Copy link
Author

Same here - I just went with the workaround :-)

@OlivierKessler01
Copy link

OlivierKessler01 commented Oct 13, 2021

image
It even breaks the standard "required" asterisk next to the labels on vertical layouts.

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot
Copy link

Friendly reminder that this issue exists. If I don't hear anything I'll close this.

@nicolas-grekas
Copy link
Member

Fixed by #46414 IIUC

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