How to apply validation constraint on file input field in symfony2?

I want to apply validation constraint on file input field of maxsize = 5mb. If user submit form with file size greater than 5 mb it should display error msg and page should not reload.

->add('fileName', 'file', array(
        'data_class' => null,
        'required' => $obj->getId() === null ? true : false,
        'label' => 'Upload File',
        'constraints' => [
                    'maxSize' => '5M',
                    'mimeTypes' => [
                        'application/pdf',
                        'application/x-pdf',
                    ],
                    'mimeTypesMessage' => 'Please upload a valid PDF',
            ]
     ));

i tried this but it is not working.


Source: forms

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.