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条)

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟