Skip to content

[TWIG] File input, visible but no text #31159

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
jean-gerard opened this issue Apr 18, 2019 · 3 comments
Closed

[TWIG] File input, visible but no text #31159

jean-gerard opened this issue Apr 18, 2019 · 3 comments

Comments

@jean-gerard
Copy link

Symfony version(s) affected: x4.2.7

Description

The bootstrap file input field is visible but has no text in it.

image

But if I add

.custom-file-input {
  opacity: 1!important;
}

The text is visible but the ugly button is too.

image

How to reproduce

I am using the bootstrap 4 template.

#config/packages/twig.yaml
twig:
    paths: ['%kernel.project_dir%/templates']
    debug: '%kernel.debug%'
    strict_variables: '%kernel.debug%'
    form_themes: ['bootstrap_4_layout.html.twig']

Generating the form like so :

$builder->add(
                'datafile',
                FileType::class,
                [
                    'label' => 'main.form.file',
                ]
);

And displaying it like so :

<div class="col-md-12 col-lg-6" id="main-form-holder">
    <div class="row form-group">
        <div class="col-md-3">{{ form_label(form.datafile) }}</div>
        <div class="col-md-9">{{ form_widget(form.datafile) }}</div>
    </div>
</div>

This problem appeared after updating from Symfony 4.0 to symfony 4.2 .

@stof
Copy link
Member

stof commented Apr 18, 2019

It looks like the label inside the Bootstrap 4 custom-form-file is set based on the placeholder attr in the form theme. As you don't set one, this is why it is empty

@jean-gerard
Copy link
Author

jean-gerard commented Apr 18, 2019

Thanks for your answer.
So, it seems that the placeholder attribute is needed, So it display something when I do this :

<div class="col-md-9">{{ form_widget(form.datafile, {'attr': {'placeholder': 'Choose a file'} }) }}</div>

Other option :

$builder->add(
                'datafile',
                FileType::class,
                [
                    'label' => 'main.form.file',
                    'attr' =>
                        [
                            'placeholder' => 'Choose a file',
                        ]
                ]
);

image

But the placeholder is not updated with the filename.
I guess i need to copy the JS from #27919 (comment) ?

Will this change in next versions ?

[EDIT] Here's my JS :

var input = $('#file_upload_datafile');
input.on( "change", function () {
    var filename = input.val().replace('C:\\fakepath\\', '').trim();
    input.parent().find('.custom-file-label').html(filename);
});

@fabpot
Copy link
Member

fabpot commented Aug 6, 2019

Closing as we are not using Github for support.

@fabpot fabpot closed this as completed Aug 6, 2019
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

5 participants