Zend Form Decorators Error in Table

六月ゝ 毕业季﹏ 提交于 2019-12-06 13:17:21
Alex Pliutau
$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'
         )
      )
   )
);

You can write your own Decorator similar to:

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;
        }
    }
}

and then use it as (in class derived from 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'))));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!