【问题标题】:Zend Decorators - Remove Id Field for DT WrapperZend 装饰器 - 删除 DT Wrapper 的 Id 字段
【发布时间】:2010-08-03 14:25:57
【问题描述】:

我有两个表单,它们共享一些 ID,因为两个输入字段称为“标题”。

Zend 为我生成了一个不错的输出,如下所示:

<dl class="zend-form">
  <dt id="title-label">
    <label for="form1-title" class="required">Description</label>
  </dt>
  <dd id="title-element">
    <input name="form1[title]" id="form1-title" value="..." type="text">
  </dd>
</dl>

现在的问题是 dt 和 dd 元素命名错误(应该是 form1-title-lable 因为这是一个子表单)。

我也尝试改变元素装饰器:

$this->addElements( ... );
$this->setElementDecorators(array(
        'ViewHelper',
        'Errors',
        array(array('data' => 'HtmlTag'),array( 'tag' => 'dd', 'class' => 'element' )),
        array(array('data' => 'Label'),array( 'tag' => 'dt', class=> 'label' ))
  ));

然而结果并不如预期。

一个标签被添加到我的提交按钮和 dt 元素的 id 仍然存在。

如何移除 id 属性?


编辑 - 元素声明:

    $titel = new Zend_Form_Element_Text('title');
    $titel->setLabel( "Title" )
          ->addValidator('NotEmpty', true)
          ->addValidator('stringLength',true, array(0, 255 ))
          ->setRequired(true)
          ->addFilter("Alnum", array(true))
          ->addFilter('StringTrim');

    $this->addElement($titel);

【问题讨论】:

  • 你在使用子表单吗?你能发布这个元素的整个代码吗?

标签: php zend-framework zend-form zend-decorators


【解决方案1】:

这听起来更像是您的子表单没有将其名称添加到 ID 中。如果你解决了这个问题,那么你就不需要删除 ID。

但是,如果您想使用 DtDdWrapper 装饰器从元素中删除 ID,您可以执行以下操作。

class Form_Foo extends Zend_Form_SubForm
{
    public function init()
    {

        $title = new Zend_Form_Element_Text('foo_title');
        $title->setLabel('Title');
        $title->removeDecorator('DtDdWrapper');
        $title->addDecorator(new Decorator_Foo());      
        $this->addElement($title);
    }
}

class Decorator_Foo extends Zend_Form_Decorator_DtDdWrapper
{
    public function render($content)
    {
        return '<dt>&nbsp;</dt>' .
               '<dd>' . $content . '</dd>';
    }
}

这应该会给你没有 ID 标签的元素。

【讨论】:

    【解决方案2】:

    您可以创建自定义标签装饰器,以便修改默认渲染功能。

    class App_Form_Decorator_Label extends Zend_Form_Decorator_Label
    {
    
        public function render()
        {
            // Insert here the render function of Zend_form_Decorator_Label but without the id decorator.
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-20
      • 2011-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-01
      • 1970-01-01
      • 2011-12-12
      相关资源
      最近更新 更多