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

Skip to content

[Form] Render form errors at the correct position in Bootstrap 4 horizontal layout #27892

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
wants to merge 1 commit into from
Closed

[Form] Render form errors at the correct position in Bootstrap 4 horizontal layout #27892

wants to merge 1 commit into from

Conversation

apfelbox
Copy link
Contributor

@apfelbox apfelbox commented Jul 7, 2018

Q A
Branch? 3.4
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #28086
License MIT
Doc PR

In the horizontal form layout for Bootstrap 4, the errors render at a weird position. This PR intends to fix it. Not sure about ordering the form error messages and the help block in 4.1, that needs to be decided by the person, that merges this fix upwards. 🙃

Before

2018-07-07 at 19 18

After

2018-07-07 at 19 18

Edit please note, that the text is in the form label container and wraps pretty early. My example screenshots don't show that too clearly, so here is another one.

2018-07-07 at 19 27

vs.

2018-07-07 at 19 28

@apfelbox
Copy link
Contributor Author

apfelbox commented Jul 7, 2018

Just updated the tests.
As I never used XPath before, I would be happy if somebody would validate my query – as it just badly copy & pasted together.

Also the test about the form in the label doesn't make sense in the horizontal form anymore. As the test is inherited from the base test class however, I need to overwrite the method. Didn't want to us markTestSkipped, as it unnecessarily displays a warning, while everything is actually okay here. 😃

@apfelbox
Copy link
Contributor Author

apfelbox commented Jul 7, 2018

@Nyholm as you added this in #24435 or at least proposed it, I would ask how we should fix the rendering issues?

Is just the ordering of label -> errors -> input (-> help) important? If this is the case, we could add the errors above the widget (and the user could reorder the with CSS flex + order.
But rendering the label inside the label with width < 200px looks a bit broken.

wdyt?

@apfelbox
Copy link
Contributor Author

apfelbox commented Jul 7, 2018

An alternative would be to just add another block

{% block form_row -%}
    {%- if expanded is defined and expanded -%}
        {{ block('fieldset_form_row') }}
    {%- else -%}
        {%- set widget_attr = {} -%}
        {%- if help is not empty -%}
            {%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
        {%- endif -%}
        <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, widget_attr) -}}
                {%- block form_widget_errors "" -%} {# <-- here! #}
                {{- form_help(form) -}}
            </div>
            {##}</div>
    {%- endif -%}
{%- endblock form_row %}

So that a user can just do this in his/her own theme:

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

{# hide label errors and display them in the widget #}
{%- block form_label_errors "" -%}
{%- block form_widget_errors -%}
    {{- form_errors(form) -}}
{%- endblock -%}

(or we combine both solutions and add the block above the {{ form_widget() }} call and the user has to reorder them, if the ordering should be widget -> errors

@apfelbox
Copy link
Contributor Author

apfelbox commented Jul 7, 2018

Travis failure is the issue, that 7.1 and 7.2 pull in a more recent version of the twig bridge, which doesn't have this change. This needs to be merged up, then this is automatically fixed.

@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Jul 8, 2018
@xabbuh
Copy link
Member

xabbuh commented Jul 9, 2018

@apfelbox For the deps=high job that's true. But we need to bump some dependencies for the deps=low job.

Status: Needs work

@apfelbox
Copy link
Contributor Author

apfelbox commented Jul 9, 2018

@xabbuh okay, so what do I need to do here? I am not sure on where I should bump the dependencies.

@xabbuh
Copy link
Member

xabbuh commented Jul 9, 2018

Looks like you can apply the same changes to composer.json as in #27886.


public function testErrorWithNoLabel()
{
// This test apply to the horizontal form, as here the errors are rendered inside the widget
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applies

Copy link
Contributor Author

@apfelbox apfelbox Jul 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"apply" was correct, but should be "doesn't apply" 😄
Will push the change after I see whether the version bumps fix the test failures.

@apfelbox
Copy link
Contributor Author

apfelbox commented Jul 10, 2018

@xabbuh after rebasing I realised, that as #27886 was now already merged, my branch should now include the changes in the composer.json.

See here: https://github.com/symfony/symfony/blob/040df30bbc655006828044cc3dab377cfc5156a9/src/Symfony/Bridge/Twig/composer.json

@apfelbox
Copy link
Contributor Author

Tests still fail though and I have now idea why.
I see that composer installs the form component, but apparently the wrong one.

As I am not familiar with how the tests work here I am not quite sure whether the test script is able to automatically install my changed dependencies.

Any help appreciated, I didn't do a cross-component PR before. 😅

/following-sibling::div[
./input[@id="name"]
/following-sibling::span[@class="alert alert-danger d-block"]
[./span[@class="mb-0 d-block"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apfelbox The XPath needs to be fixed here now (the mb-0 class was removed in #27887 in the meantime).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in fact that's now not a dependency problem of different components anymore, but just a conflict with another PR's changes. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh lol 🤦‍♀️ I just pushed the changes

@apfelbox
Copy link
Contributor Author

Travis failure is unrelated as some compilation crashed. Can I restart the travis build?

@@ -14,6 +14,8 @@
{%- endif -%}
{%- endblock form_label %}

{%- block form_label_errors "" -%}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we use the short block syntax anywhere else? Otherwise I suggest to not introduce that here. But 👍 from me for the changes here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. I only found one usage in some Tests for the short syntax, but several usages of the explicit syntax in some templates.
So I changed it to the longer version.

@stof
Copy link
Member

stof commented Jul 10, 2018

wasn't this rendered in the label for accessibility reasons ?

Fix tests for bootstrap 4 horizontal layout
@apfelbox
Copy link
Contributor Author

apfelbox commented Jul 10, 2018

@stof yep, I linked that above. That's why I would like to wait for the feedback from @Nyholm, because he made these changes and I don't want to introduce a regression here.
I also proposed an alternative solution in the comments above, which might be a good middle ground (but would require additional effort of the app developer, as (s)he would need to add a short custom form theme).

@fabpot
Copy link
Member

fabpot commented Jul 19, 2018

@Nyholm Do you have time to review this one? Would be great. Thanks.

@nicolas-grekas
Copy link
Member

(rebase needed)

@Nyholm
Copy link
Member

Nyholm commented Aug 2, 2018

I'll review this PR this week. Sorry for the delay.

@sstok
Copy link
Contributor

sstok commented Aug 4, 2018

@iluuu1994
Copy link
Contributor

Any progress on this one?

@Nyholm
Copy link
Member

Nyholm commented Sep 25, 2018

Hey. Sorry for the delay. I've finally had a sit down with an accessibility expert.

She said that it is always recommended to have the error message inside the label. A solution here would be to put the <input> inside the label as well. I know, it is weird, but that is allowed and works for most users.

I tried to argue for a different solution. The second best was to use labelled-by on the input to refer to the error message. This is only a okey solution. It is allowed by WCAG, but not optimal. It is because the support for WAI-AREA in assistive devices and browsers varies a lot and should not be trusted.

Could you see if you can update this PR to be more accessible? Now when I know the rules it is easier for me to review this PR.

@jkobus
Copy link
Contributor

jkobus commented Mar 3, 2019

@Nyholm it seems that horizontal layout is still broken (I stumbled upon this one myself).

IMHO maybe we should make it compatible with bootstrap first and then apply some sort of recommendation in the docs informing about this. For me, accessibility provided by bootstrap is very good.

@fabpot
Copy link
Member

fabpot commented Mar 4, 2019

@apfelbox @Nyholm How can we move forward on this pull request?

@apfelbox
Copy link
Contributor Author

@fabpot as @Nyholm noted, I could implement the labelled-by solution, so that we can keep using the "old" layout while having okay-ish accessibility.

This is a compromise, that should work for both sides, I guess.

I would implement it in a way, that you can override this behaviour by just overwriting two blocks. This is like my suggestion in #27892 (comment) so that you have two blocks with possible positions of the error label, and by default one is empty and the other one is filled (but you could change it by extending the template and reversing the positions).

@fabpot
Copy link
Member

fabpot commented Mar 22, 2019

@apfelbox I'm ok with your solution, but I'm no accessibility expert. @Nyholm I suggest that we implement what @apfelbox suggests if you you think this is the "best" compromise.

@Nyholm
Copy link
Member

Nyholm commented Apr 6, 2019

Thank you for this PR. I like that you addressed the issue but we also need to consider accessibility.

I suggest closing this PR and solve it like described here: #28086 (comment)

I would be more than happy to review a new PR form you.

#eu-fossa

@xabbuh xabbuh closed this Apr 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants