2012-08-27 12 views
6

Więc używam Zend i mam formularza Zend z Zend_Form_Element_File i trzy walidatorami: 1. setRequired 2. Rozszerzenie 3. WielkośćJak ustawić niestandardowy plik zend Zend Form element?

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

chcę ustawić niestandardowe komunikaty o błędach, ale dla tych walidatorami nie wiem jak.

Powodem, dla którego chcę ustawić niestandardowy komunikat o błędzie, jest to, że mam niestandardowy dekorator, za pomocą którego pobieram wszystkie błędy, gdy formularz nie jest poprawny za pomocą metody isValid() i wyświetlam je u góry formularza. Metodą, dla której łapię błędy w formularzu, jest getErrors().

Próbowałem również: http://www.mail-archive.com/[email protected]/msg25779.html wykonując:

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

i robi

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

pomocy?

Odpowiedz

18

W ten sposób ustawiam niestandardowy komunikat weryfikatora.

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

Oto niektóre z linków, które mogą okazać się przydatne w zrozumieniu komunikatu weryfikatora niestandardowego.

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

Dzięki za to! Szukałem sposobu, aby to zrobić, ale nie było to zbyt jasne w dokumentach. –

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

Aby dodać niestandardowy komunikat na zend_form_element_file, patrz poniższy kod,

$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')); 
Powiązane problemy