douti19680318 2009-06-11 03:30 采纳率: 0%
浏览 61
已采纳

在Zend Framework中,如何使用装饰器将表单元素包装在标签中?

I would like to wrap form elements with labels as such

<label for="email">E-mail <input type="text" name="email" id="email" /></label>

The only option I could find is to alter the placement; however it only accepts 'prepend' and 'append':

<!-- prepend -->
<label for="email">E-mail</label> <input type="text" name="email" id="email" />
<!-- append -->
<input type="text" name="email" id="email" /> <label for="email">E-mail</label>

That is not what I am after. I could alter the Zend_Form_Decorator_Label class, but that is the last alternative.

  • 写回答

4条回答 默认 最新

  • duannv2081 2009-06-11 03:49
    关注

    This doesn't seem to be possible unfortunately, you'll probably have to write a custom decorator. It should be pretty simple, though.

    Please note im just going to write this here, it's untested but based off another decorator so it should work with little/no modification

    class Your_Form_Decorator_Date extends Zend_Form_Decorator_Abstract {
        public function render($content) {
           $element = $this->getElement();
    
           $name = $element->getFullyQualifiedName();
           return '<label for="'.$name.'">'. $element->getLabel() . ' '.$content.'</label>';
    
        }
    }
    

    Now if you add the correct prefix to your form.

    $this->addPrefixPath('Your_Form', 'Your/Form/');
    

    You should be able to use this to wrap your input (FormElement decorator) in a label tag.

    Sorry I haven't had a chance to actually test this but given how my other decorators work. It should be fine.

    EDIT: Thanks for pointing out that the label text wasn't being rendered gnarf. This is now fixed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?