Skip to content

Removed custom classes for input="file" types #27478

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 4 commits into from
Closed

Removed custom classes for input="file" types #27478

wants to merge 4 commits into from

Conversation

lsv
Copy link

@lsv lsv commented Jun 2, 2018

Q A
Branch? master
Bug fix? HTML bug
New feature? no
BC breaks? Maybe, if frontend css are used
Deprecations? no
Tests pass? yes
Fixed tickets #27477
License MIT

Before removing these classes a form with input="file" looked like this

screenshot from 2018-06-03 00-11-28

Now after removing these

screenshot from 2018-06-03 00-28-57

@nicolas-grekas
Copy link
Member

@Nyholm that's for you :)

@nicolas-grekas nicolas-grekas added this to the 4.1 milestone Jun 4, 2018
@@ -116,7 +116,7 @@

{% block form_widget_simple -%}
{% if type is not defined or type != 'hidden' %}
{%- set attr = attr|merge({class: (attr.class|default('') ~ (type|default('') == 'file' ? ' custom-file-input' : ' form-control'))|trim}) -%}
{%- set attr = attr|merge({class: (attr.class|default('') ~ (type|default('') == 'file' ? ' ' : ' form-control'))|trim}) -%}
Copy link

Choose a reason for hiding this comment

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

should be class "form-control-file" here, not empty

Copy link
Author

Choose a reason for hiding this comment

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

You are right - Updated

@xabbuh
Copy link
Member

xabbuh commented Jun 13, 2018

@Nyholm Can you have a look here? :)

xabbuh
xabbuh previously approved these changes Jun 13, 2018
Copy link
Member

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

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

Thank you for this PR.

Could you maybe explain why this is needed?

According to bootstrap documentation for file uploads, we are adding the correct classes.

From what I see it, you want to "opt-in" to boostrap file uploads instead of having them as default, is that correct?

Now:
screen shot 2018-06-13 at 15 54 57

After this PR is merged:

screen shot 2018-06-13 at 15 55 56

Im currently 👎

@@ -187,7 +187,7 @@
{%- set element = 'legend' -%}
{%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' col-form-label')|trim}) -%}
{% elseif type is defined and type == 'file' %}
{%- set label_attr = label_attr|merge({for: id, class: (label_attr.class|default('') ~ ' custom-file-label')|trim}) -%}
{%- set label_attr = label_attr|merge({for: id, class: (label_attr.class|default('') ~ ' ')|trim}) -%}
Copy link
Member

Choose a reason for hiding this comment

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

Could be simplified to

{%- set label_attr = label_attr|merge({for: id, class: (label_attr.class|default(''))|trim}) -%}

@lsv
Copy link
Author

lsv commented Jun 13, 2018

@Nyholm I totally agree, the "design" is worse - but the UX (try selecting a file...) is much better.
And if you have like 10 file upload fields, then you properly forget which file you have selected, and which you are still needed to add.

Also, the bootstrap documentation also have the "normal unstyled" as an example
https://getbootstrap.com/docs/4.1/components/forms/#form-controls.

And if you want to have the selected file name in your file input, you will need to do something like this

jQuery(document).on('change', '.file-upload', function() {
    $(this).parent().find('label').text($(this).val().replace('C:\\fakepath\\', ''));
});

The best thing would actually be, if it was possible to opt-out (or opt-in) somehow to either set the custom or the normal CSS classes.

And then if you remove the label for the file input, which I think is quite normal if you are using multiple file uploads, then the whole file input is gone (even if you set a placeholder)

@Nyholm
Copy link
Member

Nyholm commented Jun 13, 2018

I try to leave "design" and UX to bootstrap and just follow their recommendation. But I do recognize that boostrap is not providing the best experience when using multiple file inputs.

The best thing would actually be, if it was possible to opt-out (or opt-in) somehow to either set the custom or the normal CSS classes.

I agree, that would be the best. I would prefer if we could opt-out to not change the current behavior. How would one do this properly? By just adding a css class?

@lsv
Copy link
Author

lsv commented Jun 13, 2018

Something like this?
Though, the problem is that it should be part of bootstrap4 layout, and not directly on twig.

twig:
    paths: ['%kernel.project_dir%/templates']
    debug: '%kernel.debug%'
    strict_variables: '%kernel.debug%'
    form_themes: ['bootstrap_4_layout.html.twig']
    custom_file_browser: true

custom_file_browser is true as default

Then in the form theme


{%- set attr = attr|merge({class: (attr.class|default('') ~ (type|default('') == 'file' ? ' (twig.custom_file_browser ? 'custom-control-file' : 'form-control-file') : ' form-control'))|trim}) -%}
(twig.custom_file_browser ? 'custom-control-file' : 'form-control-file')

Another option, could be directly on the FileType but again, this is not part of bootstrap

@lsv
Copy link
Author

lsv commented Jun 13, 2018

Or maybe a form theme called "bootstrap_4_layout_nocustom.html.twig,
and then just

twig:
    form_themes: ['bootstrap_4_layout_nocustom.html.twig']

@Nyholm
Copy link
Member

Nyholm commented Jun 13, 2018

Interesting. But if you configure it in yaml, then there is no why to opt-in/out on case by case basis, right?

@Nyholm
Copy link
Member

Nyholm commented Jun 13, 2018

Using "bootstrap_4_layout_nocustom.html.twig“ might be simpler.

What if you just added a "bootstrap_4_layout_nocustom_file.html.twig” (not extending anything)?

@lsv
Copy link
Author

lsv commented Jun 13, 2018

I agree on the nocustom template will be the best approach for opt-in/opt-out.

Let me give it a try, should I create a new PR or do you want it in this, but with changed back the changes I made?

@Nyholm
Copy link
Member

Nyholm commented Jun 13, 2018

Let’s keep the changes in this PR since we are trying to solve the same issue.

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jun 21, 2018

@Nyholm does this mean you're +1?

@Nyholm
Copy link
Member

Nyholm commented Jun 21, 2018

No, We are waiting for Martin to give a different implementation to fix this issue.

@xabbuh xabbuh dismissed their stale review June 21, 2018 14:21

see the discussion

@fabpot
Copy link
Member

fabpot commented Jul 19, 2018

Any news on this PR? @lsv Can you finish it or shall we close?

@lsv
Copy link
Author

lsv commented Jul 19, 2018

Im sorry, I totally forgot about this in my holiday, I will have a commit up in within the next couple of days

@lsv
Copy link
Author

lsv commented Jul 19, 2018

With this commit it will look like this

selection_106

Instead of

selection_107

How to use it change twig form theme from

twig:
    form_themes: ['bootstrap_4_layout.html.twig']

To

twig:
    form_themes: ['bootstrap_4_nocustom_layout.html.twig']

I have also removed custom check/radio boxes, though its not required as the custom styled works properly.

Also the {% block file_row -%} in bootstrap_4_layout.html.twig is wrong, the label should come before the widget, as with all other form elements. (though as the label is position absolute, it doesnt make any changes

{% block file_row -%}
    <div class="form-group">
    <{{ element|default('div') }} class="custom-file">
    {{- form_widget(form) -}}
    {{- form_label(form) -}}
    </{{ element|default('div') }}>
    </div>
{% endblock %}

I have not changed it, but my nocustom changes the label to before the widget.

@lsv
Copy link
Author

lsv commented Jul 19, 2018

@Nyholm

I will close this PR, and make a new, I can see that symfony/master has changed a lot, and if I merge master, there will be a LOT of changed files in the commit.

@lsv lsv closed this Jul 19, 2018
@lsv lsv deleted the feature/twig-remove-custom-file branch July 19, 2018 11:31
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.

7 participants