Zend:用用户定义的 html 标签包装 FormErrors [英] Zend: wrap FormErrors with user-defined html tags

查看:30
本文介绍了Zend:用用户定义的 html 标签包装 FormErrors的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Zend talk.默认情况下,使用 FormErrors 装饰器生成的错误列表具有以下标记:

Zend talk.By default,using the FormErrors decorator the generated list of errors has the following markup:

<ul class="form-errors>
 <li>
  <b>[element label or name]</b>
  <ul>
    <li>[error message]</li>
    <li>[error message]</li>
  </ul>
 </li>
</ul>

问题:如何改用以下结构?

<span class='myErrors'>
 &bull;[error message]</br>
</span>

更新:我试过:

array('FormErrors', array(
'markupListStart' => "<span class='myErrors'>",
'markupListEnd' => '</span>',
'markupListItemStart' => '',
'markupListItemEnd' => '</br>',
'ignoreSubForms'=> false,
'showCustomFormErrors' => true,
'onlyCustomFormErrors'=> false,
'markupElementLabelEnd'   => '',
'markupElementLabelStart' => ''
));

但我仍然有不需要的标签和标签.这是源代码:

But I still have unwanted tags and labels.This is the source code:

<span class='myErrors'>
 [element label or name]
 <ul class="errors">
  <li>
  [error message]
  </li>
</ul>
</br>
</span>

推荐答案

您需要两样东西:装饰器和视图助手.默认情况下,Zend Form 使用 'Errors' 装饰器,它使用 FormErrors 视图助手.您需要覆盖它以实现您的需要.以这个简单的例子来说明这一点:

You need two things: A decorator and a view helper. By default Zend Form uses the 'Errors' decorator, which uses the FormErrors view helper. You need to override this to achieve what you need. Take this simple example to illustrate this:

class Admin_Form_Authors extends Zend_Form
{

    public function init()
    {

        $name = new Zend_Form_Element_Text('name');
        $name->setLabel('name')
             ->setRequired(true)
             ->addFilter('StripTags')
             ->addFilter('StringTrim')
             ->addValidator('NotEmpty')
             ->setDecorators(array(
                'ViewHelper',
                array(new My_Form_Error_Decorator(),array('class' => 'my-error','escape' => true)),
                'Label',
                'HtmlTag'
                ));

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setAttrib('id','submitbutton');

        $this->addElements(array($name,$submit));
    }
}

如您所见,我用于 $name 表单元素的第二个装饰器是 My_Form_Error_Decorator 类的一个新对象,它看起来像这样:

As you can see, the second decorator I'm using for my $name form element, is an new object of My_Form_Error_Decorator class, which looks something like this:

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

        $errors = $element->getMessages();
        if (empty($errors)) {
            return $content;
        }

        $separator = $this->getSeparator();
        $placement = $this->getPlacement();
        $viewhelp = new My_Form_Error_View_Helper();
        $errors = $viewhelp->myErrorViewer($errors, $this->getOptions());

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

}

这实际上与默认的 'Errors' 装饰器几乎相同,我更改的唯一一行是 $errors = $viewhelp->myErrorViewer($errors, $this->getOptions()); 这里我告诉装饰器使用 My_Form_Error_View_Helper 视图助手,它看起来像这样:

This is in fact almost the same decorator as the default 'Errors' decorator, the only line I change is this $errors = $viewhelp->myErrorViewer($errors, $this->getOptions()); Here I'm telling the decorator to use the My_Form_Error_View_Helper view helper, which in turn look something like this:

class My_Form_Error_View_Helper extends Zend_View_Helper_FormElement
{
    public function myErrorViewer($errors, array $options = null)
    {

        if (empty($options['class'])) {
            $options['class'] = 'errors';
        }

        $start = "<span%s>";
        $end = "</br></span>";
        if (strstr($start, '%s')) {
            $start   = sprintf($start, " class='{$options['class']}'");
        }

        $html  = $start
               . array_pop($errors)
               . $end;

        return $html;
    }
}

我再次从原始的 FormErrors 视图助手中借用了该函数的大部分内容.这绝不是您应该实施的解决方案,但希望这会让您走上正确的道路,了解如何使用装饰器和视图助手来自定义表单的输出.我推荐以下两篇关于装饰器和视图助手的阅读(它们本身就是一个巨大的话题)

Again I borrow most of this function from the original FormErrors View Helper. This is by no means the solution you should implement, but hopefully this sets you on the right path on how you can use decorators and view helper to customize the output of your form. I recommend the following two reads about Decorators and View Helpers (They are a huge topic by themselves)

装饰器查看助手

这篇关于Zend:用用户定义的 html 标签包装 FormErrors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆