2010-09-26 62 views
2

我正在使用下一個裝飾器作爲我的輸入。我想把它作爲表格。Zend表格裝飾器錯誤表

$this->setDecorators(array('ViewHelper','Errors', 
      array(array('data'=>'HtmlTag'), array('tag' => 'td')), 
      array('Label', array('tag' => 'td')), 
      array(array('row'=>'HtmlTag'),array('tag'=>'tr')) 
    )); 

但是在表單驗證後顯示錯誤不在td中。我怎樣才能做到這一點? 我想使下妝:

<table> 
    <tr> 
     <td>Lable</td> 
     <td>Input</td> 
     <td>Error</td> 
    </tr> 
</table> 

回答

4
$this->setDecorators(
    array(
     'ViewHelper', 
     array(
     array(
      'data'=>'HtmlTag' 
     ), 
     array(
      'tag' => 'td' 
     ) 
    ), 
     array(
     'Label', 
     array(
      'tag' => 'td' 
     ) 
    ), 
     array(
     'Errors', 
     array(
      'tag' => 'td' 
     ) 
    ), 
     array(
     array(
      'row'=>'HtmlTag' 
     ), 
     array(
      'tag'=>'tr' 
     ) 
    ) 
    ) 
); 
+1

它不適合我。我檢查了源代碼,沒有'錯誤'裝飾器的'tag'參數。也許我錯過了什麼? – 2011-10-27 15:03:21

2

你可以寫你自己的裝飾類似於:

class My_Form_Decorator_ErrorsHtmlTag 
    extends Zend_Form_Decorator_Label 
{ 
    protected $_placement = 'APPEND'; 

    public function render($content) { 
     $element = $this->getElement(); 
     $view = $element->getView(); 
     if (null === $view) { 
      return $content; 
     } 

     $separator = $this->getSeparator(); 
     $placement = $this->getPlacement(); 
     $tag = $this->getTag(); 
     $tagClass = $this->getTagClass(); 
     $id = $element->getId(); 

     $errors = $element->getMessages(); 
     if (!empty($errors)) { 
      $errors = $view->formErrors($errors, $this->getOptions()); 
     } else { 
      $errors = ''; 
     } 

     if (null !== $tag) { 
      $decorator = new Zend_Form_Decorator_HtmlTag(); 
      if (null !== $tagClass) { 
       $decorator->setOptions(array(
        'tag' => $tag, 
        'id' => $id . '-errors', 
        'class' => $tagClass)); 
      } else { 
       $decorator->setOptions(array(
        'tag' => $tag, 
        'id' => $id . '-errors')); 
      } 
      $errors = $decorator->render($errors); 
     } 

     switch ($placement) { 
      case self::APPEND: 
       return $content . $separator . $errors; 
      case self::PREPEND: 
       return $errors . $separator . $content; 
     } 
    } 
} 

,然後用它作爲(從Zend_Form派生類):

$this->addPrefixPath('My_Form_Decorator', 'My/Form/Decorator/', 'decorator');  
$element->setDecorators(array(
    'ViewHelper', 
    array(array('td' => 'HtmlTag'), array('tag' => 'td')), 
    array('Label', array('tag' => 'td')), 
    array('ErrorsHtmlTag', array('tag' => 'td')), 
    array(array('tr' => 'HtmlTag'), array('tag' => 'tr'))));