3

我正在尝试制作一个构建滑块的表单。它可以有任意数量的图像,我想显示已上传图像的预览。设置多个图像字段很容易,但我正忙于显示图像的预览。

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

{% 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 %}

value变量在输入类型上始终为空file,因此我不确定如何获取上传图像的 url。我正在使用一个自定义字段类型,它只是添加一个file字段并连接数据源(这只是一个简单的包装器Symfony\Component\HttpFoundation\File\File)。如果您需要此代码,请告诉我,但它都是样板文件,所以我怀疑您是否这样做。

提前致谢。

4

1 回答 1

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 %}
于 2013-07-16T20:32:47.740 回答