Php – How to set custom error message zend form element file

elementfilePHPvalidationzend-framework

So I am using Zend and I have a Zend form with a Zend_Form_Element_File and three validators:
1. setRequired
2. Extension
3. Size

 $this->browse = new Zend_Form_Element_File('Browse');
 $this->browse->setRequired(false)->removeDecorator('errors')->removeDecorator('label')
->addValidator('Extension', true, 'pdf')->addValidator('Size', false, 2000000);

I want to set custom error messages for these validators but do not know how.

The reason I want to set a custom error message is because I have a custom decorator with which I grab all errors when the form is not valid with isValid() and display them at the top of the form. The method for which I am grabbing errors in the form is getErrors().

I've also tried: http://www.mail-archive.com/fw-general@lists.zend.com/msg25779.html
by doing:

 $validator = new Zend_Validate_File_Upload();
 $validator->setMessages(array('fileUploadErrorNoFile' => 'Upload an image!''));

and doing

 $this->browse->addValidator($validator);

Any help?

Best Answer

this is how i use to set custom validator message.

$file = new Zend_Form_Element_File('file');
$file->setLabel('File Label')
     ->setMaxFileSize('512000')
     ->addValidator('Count', true, 1)
     ->addValidator('Size', true, 512000)
     ->addValidator('Extension', true, 'jpg,jpeg,png,gif');

$file->getValidator('Count')->setMessage('You can upload only one file');
$file->getValidator('Size')->setMessage('Your file size cannot upload file size limit of 512 kb');
$file->getValidator('Extension')->setMessage('Invalid file extension, only valid image with file format jpg, jpeg, png and gif are allowed.');

here are some of the links that may prove useful to understand custom validator message.

http://framework.zend.com/manual/en/zend.validate.messages.html

Zend Framework Custom Validation Class Error Message

Can't set custom validator messages in Zend_Form