I am building a custom file upload widget where I display last uploaded filename. I created FormType class and in form/fields.html.twig
I added the following:
{% block custom_document_widget %}
{% spaceless %}
{# here I want to include code to display filename #}
{# display file input #}
{% set type = 'file' %}
{{ block('form_widget_simple') }}
{% endspaceless %}
{% endblock %}
I know that the value of a current field can be parsed {{ form.vars.value }}
, but in the end the field is file input and does not have the value of filename that was uploaded previously.
To store uploaded filename I have $filename
variable in entity and would like to display it in field widget template. How can I approach it?
In the end I had to pass the filename as an option to embedded form that represented my FileType
:
$builder
->add('resumeFile', CustomDocsType::class, array(
'required' => false,
'constraints' => array(
new File(array(
'mimeTypes' => array(
'application/pdf',
),
'mimeTypesMessage' => 'mimetype',
)),
),
'filename' => $trainee->getResumeOriginal(),
))
In my CustomDocsType:
class CustomDocsType extends AbstractType
{
public function buildView(FormView $view, FormInterface $form, array $options)
{
parent::buildView($view, $form, $options);
$view->vars = array_merge($view->vars, array(
'filename' => $options['filename']
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'filename' => null
));
}
public function getParent()
{
return FileType::class;
}
}
And now I only had to acces the filename in template:
{{ form.vars.filename }}