tags:

views:

1874

answers:

4
+1  A: 

You seem to be using the API incorrectly. The signature of sfForm::setValidator() is here.

However, here's a simple way to set a file validator that allows only web images to be uploaded:

$this->validatorSchema['my_file'] = new sfValidatorFile(array(
    'mime_types' => 'web_images'
), array(
    'invalid'    => 'Invalid file.',
    'required'   => 'Select a file to upload.',
    'mime_types' => 'The file must be of JPEG, PNG or GIF format.'
));

The 'web_images' category includes the following MIME types:

image/jpeg
image/pjpeg
image/png
image/x-png
image/gif

If you want to explicitly define the MIME types to accept, replace 'web_images' with an array of types like this:

'mime_types' => array(
    'image/jpeg',
    'image/pjpeg',
    'image/png',
    'image/x-png',
    'image/gif'
)
Ree
Thanks, but my mime type shouldn't just include image only; I also need to include zip files, or word document files
Ngu Soon Hui
OK, from your question it would seem like you wanted images only. I have edited my answer with an explanation on how to set custom MIME types.
Ree
Unfortunately it doesn't seem to work; I have updated the question
Ngu Soon Hui
It doesn't seem to work in the sense that I want client side validation-- but it does work if you want server side validation. And client side validation specs are never properly implemented in the most browsers.
Ngu Soon Hui
A: 

I tried using BMP Images but it's not showing any invalid message. I am using web_images.

Ankit Shah
A: 

BMP aren't web images

Tofuwarrior
A: 

There is a way to constraint file selection from upload in a browser.
The drawback is it uses flash to select file, and limit support to users having flash player installed. The advantage is flash give you much more control over upload (parrallel upload, progression, errors, controle files extension allowed to select, etc.).

The component I use for this is swfupload

EDIT : this does not exempt from doing server-side control on uploaded files, but used in combination it offers a similar security leel and a much better user experience (errors can happens before a long-running upload start)

Benoit