Symfony2 + CreateFormBuilder如何在窗体中渲染图像 [英] Symfony2 + CreateFormBuilder how to render an image in a form

查看:147
本文介绍了Symfony2 + CreateFormBuilder如何在窗体中渲染图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



目前我正在使用 {{form_widget(form)}} code>来显示表单。
我的实体具有 path 属性,它是文件系统上映像保存的路径。
我想在表单中显示图片(使用< img> html标签),我怎样才能达到这个效果?我是否应该按字段处理模板中的表单?还是有办法在模板中只拥有一个字段,并使用 {{form_widget(form))}

解决方案

你可以做的是按字段处理表单字段,如果设置了值,则显示图像。



假设你有一个字段名称 image 。在Twig中,您可以通过 form.vars.value.image 来访问它的值。然后,显示一个img标签很容易:

  {%如果form.vars.value.image不为空%} 
< img src ={{asset('upload / dir /'〜form.vars.value.image)}}/>
{%endif%}

这里, upload / dir / 是存储图像的路径。如果你有这个路径的常量,你可以在Twig中使用它:

  {{asset(constant('Acme \ \DemoBundle\\Model\\\\Object:UPLOAD_DIR')〜'/'~form.vars.value.image)}} 

另一种方法是使用自己的模板创建自己的类型:



编辑:我忘了一个有趣的选择。您可以自定义单个字段: http://symfony.com/doc/current/cookbook/form/form_customization.html#how-to-customize-an-individual-field 。这是您可以做的草稿:

  {%form_theme form _self%} 

{ %block_object_image_row%}
< div class =name_row>
{{form_label(form)}}
{{form_errors(form)}}
{{form_widget(form)}}

{%if form.vars .value.image不为空%}
< img src ={{asset('upload / dir /'〜form.vars.value.image)}}/>
{%endif%}
< / div>
{%endblock%}


I'm Symfony2 and CreateFormBuilder to create my form.

Currently I'm using {{ form_widget(form) }} to display the form. My entity have path property that is the path of an image save on filesystem. I want to display the image in the form (with a <img> html tag), how can I achieve this result? Should I handle the form in my template field by field? Or is there a way to own only one field in the template and render the other ones with {{ form_widget(form) }} ?

解决方案

What you could do is handling the form field by field, and display the image if the value is set.

Let's say you have a field name image. In Twig, you can access its value through form.vars.value.image. Then, it's quite easy to display an img tag:

{% if form.vars.value.image is not null %}
    <img src="{{ asset('upload/dir/' ~ form.vars.value.image) }}" />
{% endif %}

Here, upload/dir/ is a path where you store your images. If you have a constant for this path, you can use it in Twig:

{{ asset(constant('Acme\\DemoBundle\\Model\\Object::UPLOAD_DIR') ~ '/' ~ form.vars.value.image) }}

An alternative could be to create your own type with its own template:

Edit: I forgot an interesting alternative. You can customize an individual field: http://symfony.com/doc/current/cookbook/form/form_customization.html#how-to-customize-an-individual-field. Here is a draft of what you could do:

{% form_theme form _self %}

{% block _object_image_row %}
    <div class="name_row">
        {{ form_label(form) }}
        {{ form_errors(form) }}
        {{ form_widget(form) }}

        {% if form.vars.value.image is not null %}
            <img src="{{ asset('upload/dir/' ~ form.vars.value.image) }}" />
        {% endif %}
    </div>
{% endblock %}

这篇关于Symfony2 + CreateFormBuilder如何在窗体中渲染图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆