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 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错