2
votes

I added a VichImageType field on edit profile twig template...so im trying to check image dimensions using vich_uploader.pre_upload as Event.

In my class i got the image properties and if their dimensions arent bigger enough i tried to stop propagation and flashed a message to the twig template but, i dont know why, the event keeps propagating and redirects to fos_user_profile_show showing the image setup. Also, i tried to redirect again to fos_user_profile_edit but i cant use $event because "Vich\UploaderBundle\Event\Event" doesnt implement setController(). How can achieve this?

This is the method of the Listener class:

namespace BackendBundle\EventListener;

use Vich\UploaderBundle\Event\Event;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;

class ComprobarDimensionesDeImagen
{

private $requestStack;
private $router;

public function __construct(RequestStack $requestStack, RouterInterface $router)
{
    $this->requestStack = $requestStack;
    $this->router = $router;
}

public function onVichUploaderPreUpload(Event $event)
{
    $object = $event->getObject();
    $mapping = $event->getMapping();

    $imagen = getimagesize($object->getImageFile());

    if (250 > $imagen[0] || 250 > imagen[1]) {
        $request = $this->requestStack->getCurrentRequest();
        $session = $request->getSession();

        $event->stopPropagation();
        $session->getFlashBag()->add('error', "Minimum dimensions: 250x250 \n");

        $url = $this->router->generate('fos_user_profile_edit');
        /*
         * Testing different methods of redirect
         * 
         * $response = new RedirectResponse($url);
         * $event->setResponse($response);
         */

        $event->setController(function() use ($request) {
            return new RedirectResponse($url);
        });            
    }
}
}

When i edit the profile again, I can see the flash message and the image setup in VichImageType field (i didnt expect that stopping propagation). Any help will be very welcome.

SOLVED: Just using @Assert\Image in my Entity class did the validation. No service neither listener needed

1

1 Answers

1
votes

The argument for ->setController must be a callable . In your case, the function you pass as an argument returns an object of type Response. In order to be callable, the method should have the suffix Action. See also this post.