Vichuploaderbundle: Constraints / Asserts on UploadableField

Created on 29 Mar 2016  路  4Comments  路  Source: dustin10/VichUploaderBundle

Hello,

Thanks for the bundle, the upload works wonderfully. However, I'm trying to put a constraint on the uploadedFile for instance :

use Symfony\Component\Validator\Constraints as Assert;
    /**
     * NOTE: This is not a mapped field of entity metadata, just a simple property.
     * 
     * @Assert\Image{
     *      maxWidth=400,
     *      maxHeight=400
     * }
     * @Vich\UploadableField(mapping="user_avatar", fileNameProperty="avatarName")
     * 
     * @var File
     */
    private $avatarFile;

But the constraint is not working and i can upload image of whatever dimensions, how should I fix that ? Thanks.

Most helpful comment

If you are using a CollectionType for your uploads on your form you'll need to use the Valid constraint on the property on the parent entity.

All 4 comments

How do use it in the your form? Your exact snippet worked on my tests.

Hi,

I use it as the following :

        if ($editForm->handleRequest($request)->isValid()) {
            //$this->getDoctrine()->getManager()->flush();
            $userManager = $this->get('fos_user.user_manager');
            $userManager->updateUser($user);
            return $this->redirect($this->generateUrl('users'));
        }

I use FOSUserBundle to handle users and such, so if you said it's working it's something with fosuser validation :(

Thanks

If you are using a CollectionType for your uploads on your form you'll need to use the Valid constraint on the property on the parent entity.

You have to use parenthesis instead of brackets :

* @Assert\Image(
     *      maxWidth=400,
     *      maxHeight=400
     * )
Was this page helpful?
0 / 5 - 0 ratings

Related issues

benIT picture benIT  路  3Comments

gonzalovilaseca picture gonzalovilaseca  路  7Comments

petrjirasek picture petrjirasek  路  7Comments

andrea-daru picture andrea-daru  路  3Comments

Propscode picture Propscode  路  4Comments