【问题标题】:VichUploaderBundle: Disable automatic image render in twig formVichUploaderBundle:禁用树枝形式的自动图像渲染
【发布时间】:2018-12-26 10:11:46
【问题描述】:

如何在使用 VichUploaderBundle 时禁用自动图像渲染?我以树枝形式单独显示图像,因此不需要 VichUploaderBundle 来渲染图像。

我的代码如下:

/**
 * @Route("/testupload", name="testupload")
 */
public function testUploadAction(Request $request){

    $testUpload = new TestUpload();
    $em = $this->getDoctrine()->getManager();

    $form = $this->createFormBuilder($testUpload)
        ->add('imageFile', VichImageType::class, array(
            'label'             => false,
            'required'          => false,
            'image_uri'         => true,
            'download_link'     => false
        ))
        ->add('upload',SubmitType::class, array('label' => 'Upload'))
        ->getForm();

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $testUpload = $form->getData();
        $em->persist($testUpload);
        $em->flush();
    }

    $images = $em->getRepository(TestUpload::class)->findAll();

    return $this->render('main/rotta_upload_test.html.twig', [
        'title' => 'Upload test',
        'images' => $images,
        'form' => $form->createView()
    ]);

}

树枝模板

<div class="large-8 medium-8 cell">
    <div class="callout">

        {{ form_start(form) }}            
        {{ form_widget(form) }}
        {{ form_end(form) }}

        {% for image in images %}
            <img src="{{ asset(vich_uploader_asset(image, 'imageFile')) 
}}" alt="image 1"  width="300" />
        {% endfor %}

    </div>
</div>

感谢任何帮助。

【问题讨论】:

  • 将 image_uri 设置为 false
  • 我将评论转换为完整答案。如果你愿意,你可以接受它。

标签: php symfony twig vichuploaderbundle


【解决方案1】:

只需将 image_uri 设置为 false 即可:

$form = $this->createFormBuilder($testUpload)
        ->add('imageFile', VichImageType::class, array(
            'label'             => false,
            'required'          => false,
            'image_uri'         => false,
            'download_link'     => false
        ))
        ->add('upload',SubmitType::class, array('label' => 'Upload'))
        ->getForm();

查看 form_theme (https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/views/Form/fields.html.twig#L34) 中的相关部分:

{% block vich_image_widget %}
    {% spaceless %}
        <div class="vich-image">
            {{ form_widget(form.file) }}
            {% if form.delete is defined %}
                {{ form_row(form.delete) }}
            {% endif %}

            {% if image_uri %}
                <a href="{{ image_uri }}"><img src="{{ image_uri }}" alt="" /></a>
            {% endif %}
            {% if download_uri %}
                <a href="{{ download_uri }}">{{ translation_domain is same as(false) ? download_label : download_label|trans({}, translation_domain) }}</a>
            {% endif %}
        </div>
    {% endspaceless %}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-30
  • 1970-01-01
  • 2012-07-27
  • 1970-01-01
  • 2016-04-18
  • 2015-12-10
  • 1970-01-01
相关资源
最近更新 更多