How to install custom zend form error message file?

So, I use Zend, and I have a Zend form with 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 install custom error messages for these validators, but I don’t know how to do it.

The reason I want to set my own error message is because I have my own decorator with which I get all errors when the form is invalid with isValid () and displays them at the top of the form. The method for which I take errors in the form is getErrors ().

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

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

and making

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

Any help?

+6
source share
3 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 you might find useful in understanding a custom validator message.

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

Zend Framework Authentication Class Error Message

Unable to configure custom validation messages in Zend_Form

+18
source
 $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')); 
+2
source

To add a custom message to zend_form_element_file, see the 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')); 
0
source

Source: https://habr.com/ru/post/923912/


All Articles