Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set custom error message zend form element file?

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/[email protected]/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?

like image 724
SoluableNonagon Avatar asked Aug 27 '12 21:08

SoluableNonagon


2 Answers

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

like image 85
Ibrahim Azhar Armar Avatar answered Oct 17 '22 02:10

Ibrahim Azhar Armar


$this->browse = new Zend_Form_Element_File('Browse');
$this->browse->setRequired(true)
             ->removeDecorator('errors')
             ->removeDecorator('label')
             ->addValidator('Extension', true, 'pdf')
             ->addValidator('Size', false, 2000000)
             //->setMessage('You custom message')
             ->addValidator('File_Upload', true, array('messages'=>'You custom message'));
like image 2
Daya Avatar answered Oct 17 '22 00:10

Daya