1
votes

I know that the prefered way of uploading files is via Doctrine as explained in the book, but i need to save image data in an xml file and I also like constraint validator. Not much code has to be written for them. Anyway, I'm stuck on the Image constraint and i don't know am i doing everything the right way.

My validator.yml is as folows...

Frizer\AdminBundle\Entity\UploadedImage:
    properties:
        image:
            - Image:
                 notFoundMessage: Image not uploaded
                 uploadErrorMessage: The image could not upload. Try again
                 uploadIniSizeErrorMessage: Image has to  have more that 5M
                 mimeTypes: [image/jpeg, image/jpg, image/png]
                 mimeTypesMessage: Image has to be either jpg, jpeg or png format
                 minWidth: 500
                 minHeight: 500
                 minWidthMessage: Image has to be 500/500 px 
                 minHeightMessage: Image has to be 500/500px
                 maxSize: 5M
                 maxSizeMessage: Image has to have more than 5M

validate() method accepts a UploadedImage object that has an $image property and a setter method for that property with a File type attribute. The constructor is as follows...

    $uploadedImage = new UploadedImage();

    $validator = $this->get('validator');
    $errors = $validator->validate($uploadedImage);

    var_dump($errors);

If i upload an image bigger that 5M, $errors is empty. Same thing if i don't upload anything. I searched trought the web and stack overflow with a tutorial on how to use constraints in file uploads but with no success. Could anyone explain what am I doing wrong?

1

1 Answers

0
votes

Judging by how you're create a new UploadedImage entity and attempt to validate it immediately, I suspect you aren't handling the form data correctly.

Look at Symfony2 file upload step by step -- it outlines doing an upload as well, but uses annotations for validation.

You don't have to use doctrine, so you can ignore any of the @ORM annotations. You'll just have to output whatever you need to output before the request is done.

Also, I'm not sure if its how it pasted in, but check your indentation levels in validation.yml (notFoundMessage, uploadErrorMessage, etc., are not indented correctly)

Frizer\AdminBundle\Entity\UploadedImage:
    properties:
        image:
            - Image:
                notFoundMessage: Image not uploaded
                ...