-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Improve rendering of file
field in bootstrap 4
#27919
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
Conversation
file
field in bootstrap 4file
field in bootstrap 4
$this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'my&class form-control-file')), | ||
'/input | ||
[@type="file"] | ||
$this->assertWidgetMatchesXpath($form->createView(), array('id' => 'nope', 'attr' => array('class' => 'my&class form-control-file')), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know that this id
-thing here is weird, but the check doesn't make sense for this file type (because of the special structure), I guess.
WIll this fix #27477? |
@xabbuh unfortunately not ... that doesn't even work in their own docs in the examples: https://getbootstrap.com/docs/4.0/components/forms/#file-browser So imo we have two choices regarding #27477 1. Say that the user has to do it themselvesJust like they need to load the CSS by themselves, they would need to write some JS as integration code here. We can maybe make it easier for them by adding a hint in the docs with example code. 2. add the JS functionality with a possibility to opt-outOpt-out because inline JS and CSP don't mix and because somebody doesn't want custom JS in their app (not sure whether Symfony 2+ ever injected JS somewhere else besides the toolbar). Would look something like this: {% block file_widget -%}
<div class="form-group">
...
</div>
<script type="text/javascript">
(function (document) {
var input = document.getElementById("{{form.vars.id}}");
var placeholder = input.parentElement.querySelector("custom-file-label.");
input.addEventListener(
"change",
function ()
{
var value = input.value.replace('C:\\fakepath\\', '').trim();
placeholder.textContent = "" !== value ? value : (input.getAttribute('placeholder') || '');
}
);
})(document);
</script>
{% endblock %} if we want to set it directly on each field or add some JS at the end of the form that does the same thing. |
This issue right here is only for fixing the styling of the form row containing the file field. |
This should be for 3.4 since it's a bug, I guess? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With these changes we'll have 2 <label>
tags for this field, which is not appropriate according to this stackoverflow answer. We can have <legend>
for the first one, WDYT?
@vudaltsov the file field in the bootstrap 4 Theme was added in 4.1. |
@vudaltsov I am afraid that you read the StackOverflow answer wrong.. Here is what I read there:
One label can only have one input.
One input can have multiple labels. We fall in the second category. |
Great! I was wrong. Thank you for explanations 👍 |
Thank you @apfelbox. |
…pfelbox) This PR was squashed before being merged into the 4.1 branch (closes #27919). Discussion ---------- [Form] Improve rendering of `file` field in bootstrap 4 | Q | A | ------------- | --- | Branch? | 4.1+ | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | — | License | MIT | Doc PR | — Hi 👋 currently adding a file type to a form looks weird in the bootstrap 4 form themes: ```php $builder ->add("pdfFile", FileType::class, [ "label" => "PDF", ]); ``` ## Before ### Vertical Form  ### Horizontal Form  ## After ### Vertical Form  ### Horizontal Form  ## Things to consider There are three labels here:  1) the actual field label. Here the `$options["label"]` is used. 2) the placeholder. Here `$options["attr"]["placeholder"] ?? ""` is used **new** 3) the label on the button. This is set in CSS actually, on an `::after` attribute. There isn't much we can do about 3) because there is no inline HTML-way to overwrite the style of a pseudo element. So if the app developer wants to have localization in this field as well, (s)he has to set it in their CSS file [as described in the bootstrap docs](https://getbootstrap.com/docs/4.0/components/forms/#file-browser) ## Todo - [x] ~~WIP because I haven't managed to get the test suite to run locally yet, so we will debug with Travis 🙌~~ okay, getting them to work is really, really easy, but you just have to get over yourself and do it 🙄 Commits ------- 32ad5d9 [Form] Improve rendering of `file` field in bootstrap 4
…lbox) This PR was merged into the 4.1 branch. Discussion ---------- [Form] Remaining changes for bootstrap 4 file fields | Q | A | ------------- | --- | Branch? | 4.1 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | — | License | MIT | Doc PR | — Hi again, This is a follow-up PR for #27919 Apparently I talked about it in the previous PR, but forgot to actually push the change. Sorry about that! 😞 This PR no actually adds this instead of just talking about it:  Sorry again, just forgot to actually update the last PR. Commits ------- 3cd2eef Add placeholder support in bootstrap 4 file fields
…ap 4 (apfelbox) This PR was squashed before being merged into the 4.1 branch (closes symfony#27919). Discussion ---------- [Form] Improve rendering of `file` field in bootstrap 4 | Q | A | ------------- | --- | Branch? | 4.1+ | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | — | License | MIT | Doc PR | — Hi 👋 currently adding a file type to a form looks weird in the bootstrap 4 form themes: ```php $builder ->add("pdfFile", FileType::class, [ "label" => "PDF", ]); ``` ## Before ### Vertical Form  ### Horizontal Form  ## After ### Vertical Form  ### Horizontal Form  ## Things to consider There are three labels here:  1) the actual field label. Here the `$options["label"]` is used. 2) the placeholder. Here `$options["attr"]["placeholder"] ?? ""` is used **new** 3) the label on the button. This is set in CSS actually, on an `::after` attribute. There isn't much we can do about 3) because there is no inline HTML-way to overwrite the style of a pseudo element. So if the app developer wants to have localization in this field as well, (s)he has to set it in their CSS file [as described in the bootstrap docs](https://getbootstrap.com/docs/4.0/components/forms/#file-browser) ## Todo - [x] ~~WIP because I haven't managed to get the test suite to run locally yet, so we will debug with Travis 🙌~~ okay, getting them to work is really, really easy, but you just have to get over yourself and do it 🙄 Commits ------- 32ad5d9 [Form] Improve rendering of `file` field in bootstrap 4
…s (apfelbox) This PR was merged into the 4.1 branch. Discussion ---------- [Form] Remaining changes for bootstrap 4 file fields | Q | A | ------------- | --- | Branch? | 4.1 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | — | License | MIT | Doc PR | — Hi again, This is a follow-up PR for symfony#27919 Apparently I talked about it in the previous PR, but forgot to actually push the change. Sorry about that! 😞 This PR no actually adds this instead of just talking about it:  Sorry again, just forgot to actually update the last PR. Commits ------- 3cd2eef Add placeholder support in bootstrap 4 file fields
Hi @apfelbox, thank you for this and #27958. I just updated to Symfony 4.1.3 in the hope of using it but I still see the 4.1.1 behavior : no label above the field. In my case the code $builder->add('name', TextType::class, [
'label' => 'Nom du partenaire'
])
->add('image', FileType::class, [
'label' => 'LABEL',
'attr' => ['placeholder' => 'PLACEHOLDER'],
'mapped' => false,
]); Looking at the source code in my |
Hmm, not that I know of, so it should work. Do you have any custom form themes loaded and how do you render the form in your template? |
@apfelbox a quick follow-up : I noticed that the {% block file_widget -%}
{# this is the wrapper i'm talking about #}
<div class="form-group">
<{{ element|default('div') }} class="custom-file">
{%- set type = type|default('file') -%}
{{- block('form_widget_simple') -}}
<label for="{{ form.vars.id }}" class="custom-file-label">
{%- if attr.placeholder is defined -%}
{{- translation_domain is same as(false) ? attr.placeholder : attr.placeholder|trans({}, translation_domain) -}}
{%- endif -%}
</label>
</{{ element|default('div') }}>
</div>
{% endblock %} In bootstrap, the So when the (this might be easier to see without the Chrome console info) |
@MrMitch ahaha, you were faster than me 😉 |
… in bootstrap 4 (MrMitch) This PR was merged into the 4.1 branch. Discussion ---------- [Form] Remove extra .form-group wrapper around file widget in bootstrap 4 | Q | A | ------------- | --- | Branch? | 4.1 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a This is a follow-up to #27958 and #27919 by @apfelbox . It fixes an extra space between the help text of a FileType widget and the widget itself. The extra space was caused by a `.form-group` wrapper in the `file_widget` block. Commits ------- 01e7fe4 [Form] Remove extra .form-group wrapper around file widget in bootstrap 4
…ap 4 This is a follow-up to symfony/symfony#27958 and symfony/symfony#27919. It fixes an extra space between the help text of a FileType widget and the widget itself. The extra space was cause by a .form-group wrapper in the file_widget block.
It works in my project |
Hi 👋
currently adding a file type to a form looks weird in the bootstrap 4 form themes:
Before
Vertical Form
Horizontal Form
After
Vertical Form
Horizontal Form
Things to consider
There are three labels here:
$options["label"]
is used.$options["attr"]["placeholder"] ?? ""
is used new::after
attribute.There isn't much we can do about 3) because there is no inline HTML-way to overwrite the style of a pseudo element. So if the app developer wants to have localization in this field as well, (s)he has to set it in their CSS file as described in the bootstrap docs
Todo
WIP because I haven't managed to get the test suite to run locally yet, so we will debug with Travis 🙌okay, getting them to work is really, really easy, but you just have to get over yourself and do it 🙄