Symfony 2:"文件"字段值始终为空

The*_*iac 2 symfony

我正在尝试制作一个构建滑块的表单.它可以有任意数量的图像,我想显示已经上传的图像的预览.设置多个图像字段非常简单,但我已经开始着眼于显示图像的预览.

我正在使用此模板呈现"滑块图像"字段:

{% block form_widget_simple %}
{% spaceless %}
    <div class="form-widget slider">
        {% set type = type|default('text') %}
        {% if type == 'file' and value is not empty %}
            <img src="{{ value }}" width="200"/><br/>
        {% endif %}
        <input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
    </div>
{% endspaceless %}
{% endblock form_widget_simple %}
Run Code Online (Sandbox Code Playgroud)

value变量始终是空的file输入类型,所以我不知道我怎么可以在上传的图片的URL得到.我正在使用一个自定义字段类型,它只是添加一个file字段并挂钩数据源(这只是一个简单的包装器Symfony\Component\HttpFoundation\File\File).如果你需要这个代码让我知道,但它的所有样板东西所以我怀疑你这样做.

提前致谢.

Nor*_*icz 6

Symfony2 FileType没有值,它在buildView方法中被写入. https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/FileType.php#L28

但是您可以通过forms.vars.data访问它

    {% if type == 'file' and form.vars.data is not null %}
        {# Here you should somehow generate url to file but let assume 
           you have it in "/path/to/file/" folder that is accessible through web server #}
        <img src="/path/to/file/{{ form.vars.data.name }}" /><br/>
    {% endif %}
Run Code Online (Sandbox Code Playgroud)