2

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

v0r73x
  • 21
  • 4

1 Answers1

1

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.

  • Sorry, i dont get it. Now I know that $event parameter is not callable but its the object returned by the event `vich_uploader.pre_upload`. How can i redirect to fos_user_profile_edit to workaround that event keeps propagating or throw a form exception? – v0r73x Aug 11 '17 at 07:28
  • No, I was talking about the function() you pass as the argument for `$event->setController()` . as stated in the documentation, this argument must be a callable, which I believe your anonymous function (or what it returns) is not. – Denis St-Michel Aug 11 '17 at 10:03
  • 1
    OK, thank you. Anyway, my approaching to the problem was wrong. I just needed to use Image constraint with Assert in my entity class linked to the Form instead of the complicated path I chose. That way Form's validation do the thing. Thank you for your replies. – v0r73x Aug 11 '17 at 10:56
  • Yep, true, best way to do it. Good luck with your project! – Denis St-Michel Aug 11 '17 at 15:41