表中的Zend表单装饰器错误

时间:2010-09-26 07:27:22

标签: php zend-framework html-table zend-form

我正在使用下一个装饰器来输入。我想把它作为表格。

$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>

2 个答案:

答案 0 :(得分: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 :(得分: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'))));