2012-08-29 54 views
0

當我們向zend中的表單元素添加裝飾器時,驗證消息不顯示爲什麼?Zend_Form裝飾器:無法查看錯誤消息(驗證)

代碼示例:

$this->addElement('Text', 'Last_Name',array(
     //'decorators' => $this->elementDecoratorsTr, 
     'label' => 'Last Name:', 
     'required' => false, 
     'filters' => array('StringTrim'), 
      'validators' => array(array('validator' => 'StringLength','validator' => 'Alpha')) 
     )); 

回答

1

這裏是Zend_Form_Element源代碼:

$decorators = $this->getDecorators(); 
if (empty($decorators)) { 
    $this->addDecorator('ViewHelper') 
     ->addDecorator('Errors') // notice Errors decorator 
     ->addDecorator('Description', array('tag' => 'p', 'class' => 'description')) 
     ->addDecorator('HtmlTag', array('tag' => 'dd', 
             'id' => $this->getName() . '-element')) 
     ->addDecorator('Label', array('tag' => 'dt')); 
} 

如果您設置自己的裝飾,則默認設置會不會被加載。

爲了看到驗證消息,您需要在您設置的裝飾器中包含一個Errors裝飾器。

+0

嗨ainokna,是的......我沒有錯誤在我自己的裝飾器裝飾....現在我已經添加了錯誤裝飾及其工作...謝謝你... – Punee

0

這裏是和榜樣對於錯誤味精設置裝飾:

我們在有元素:

$title = $this->createElement('text', 'title'); 
    $title->setRequired(true) 
     ->setLabel('Title:') 
     ->setDecorators(FormDecorators::$simpleElementDecorators) 
     ->setAttrib('maxlength', $validationConfig->form->title->maxlength) 
     ->addValidator('stringLength', false, array($validationConfig->form->title->minlength, 
       $validationConfig->form->title->maxlength, 
       'encoding' => 'UTF-8', 
       'messages' => array(
        Zend_Validate_StringLength::INVALID => 
        'Title must be between %min% and %max% characters', 
        Zend_Validate_StringLength::TOO_LONG => 
        'Title cannot contain more than %max% characters', 
        Zend_Validate_StringLength::TOO_SHORT => 
        'Title must contain more than %min% characters')));      
    $this->addElement($title); 

,這是類形式的裝飾,你可以做很多他們的存在:

class FormDecorators { 
    public static $simpleElementDecorators = array(
     array('ViewHelper'), 
     array('Label', array('tag' => 'span', 'escape' => false, 'requiredPrefix' => '<span class="required">* </span>')), 
     array('Description', array('tag' => 'div', 'class' => 'desc-item')), 
     array('Errors', array('class' => 'errors')), 
     array('HtmlTag', array('tag' => 'div', 'class' => 'form-item')) 
    ); 
    }