1

How do I disable the automatic image render when using VichUploaderBundle? I am displaying the images separately in my twig form, so do not need VichUploaderBundle to render the image.

My code can be seen below:

/**
 * @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()
    ]);

}

Twig template

<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>

Any assistance is appreciated.

Alex
  • 878
  • 3
  • 16
  • 34

1 Answers1

3

Just set image_uri to false will prevent it:

$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();

see relevant part in the 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 %}
Rufinus
  • 29,200
  • 6
  • 68
  • 84
  • 1
    @Sébastien Grognuz, if an answer solves your issue, you can accept it. Please see [How does accepting an answer work?](https://meta.stackexchange.com/a/5235/388672). Thank you for the future readers. – Syscall Feb 27 '22 at 12:00