2012-08-27 19 views
6

तो मैं Zend का उपयोग कर रहा हूँ और मैं एक Zend_Form_Element_File और तीन प्रमाणकों के साथ एक Zend फार्म है: 1. setRequired 2. एक्सटेंशन 3. आकारकस्टम त्रुटि संदेश ज़ेंड फ़ॉर्म तत्व फ़ाइल कैसे सेट करें?

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

मैं इन प्रमाणकों के लिए कस्टम त्रुटि संदेश सेट करना चाहते हैं, लेकिन पता नहीं कैसे।

कारण मैं एक कस्टम त्रुटि संदेश सेट करना चाहता हूं क्योंकि मेरे पास एक कस्टम सजावट है जिसके साथ मैं सभी त्रुटियों को पकड़ता हूं जब फॉर्म isValid() के साथ मान्य नहीं है और उन्हें फ़ॉर्म के शीर्ष पर प्रदर्शित करता है। जिस विधि के लिए मैं फॉर्म में त्रुटियों को पकड़ रहा हूं वह प्राप्तकर्ता है()।

मैं भी कोशिश की है: http://www.mail-archive.com/[email protected]/msg25779.html करके:

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

और कर

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

किसी भी मदद की?

उत्तर

18

इस प्रकार मैं कस्टम सत्यापनकर्ता संदेश सेट करने के लिए उपयोग करता हूं।

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

यहां कुछ लिंक हैं जो कस्टम सत्यापनकर्ता संदेश को समझने के लिए उपयोगी साबित हो सकते हैं।

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

+0

धन्यवाद! ऐसा करने के लिए एक रास्ता तलाश रहा था लेकिन यह दस्तावेज़ों में अत्यधिक स्पष्ट नहीं था। –

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

इस के लिए zend_form_element_file पर कस्टम संदेश जोड़ें करने के लिए निम्न कोड देखते हैं,

$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')); 
संबंधित मुद्दे