How to set custom error message zend form element file?

15,814

Solution 1

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

Solution 2

$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'));

Solution 3

To add custom message on zend_form_element_file, see following code,

   $browse = new Zend_Form_Element_File('Browse');
   $browse->addValidator('Extension', false, array('pdf',
               'messages'=>array('fileExtensionFalse'=>'file extension is not supported'))
          ->addValidator('Size', false, array(2000000,
                'messages'=>array('filesizefalse'=>'maximum 2000000 supported'));
Share:
15,814
SoluableNonagon
Author by

SoluableNonagon

Current Web Developer

Updated on June 16, 2022

Comments

  • SoluableNonagon
    SoluableNonagon almost 2 years

    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?