【问题标题】:Removing decorators on Zend form elements by parameters通过参数删除 Zend 表单元素上的装饰器
【发布时间】:2009-07-06 16:21:31
【问题描述】:

Zend 自动在它生成的表单元素周围添加标签。如何删除这些标签作为 addElement 函数的参数。

我已尝试更改 disableLoadDefaultDecorators 标志,但该元素根本不渲染。

例如: $searchForm->addElement('text', 'searchText', array('class'=>'onClickClear', 'disableLoadDefaultDecorators' => true));

【问题讨论】:

  • 通过禁用默认装饰器,您将禁用所有装饰器。因此,即使是呈现元素、其标签和错误装饰器的那个。你想要什么究竟?定义列表包装器?

标签: php zend-framework zend-form


【解决方案1】:

您可以通过传递一组装饰器来加载来覆盖 createElement / addElement 中的默认装饰器。

“ViewHelper”装饰器通常呈现表单元素本身,“错误”用于验证器问题,“标签”用于表单元素通常很方便。

$searchForm->addElement('text', 'searchText', array(
  'class'=>'onClickClear', 
  'decorators'=>Array(
    'ViewHelper',
    'Error', 
    array('Label', array('tag' => 'div')),
   ),
));

【讨论】:

  • 在代码中我认为装饰器名称是'Errors'而不是'Error' :-)
【解决方案2】:

另一种方法是在初始化表单后立即调用 setElementDecorators(),它会为所有后续元素设置默认装饰器。我将下面的代码用于非常简单的(一个或两个字段表单),我只想显示在一行上并且不需要大量验证:

$form = new Zend_Form();
$form->setElementDecorators( array( 'ViewHelper', 'Label' ) );

【讨论】:

    【解决方案3】:

    我认为这将有助于删除 HtmlTag 装饰器:

    $element = $searchForm->createElement('text', 'searchText', array('class'=>'onClickClear'));
    $element->removeDecorator('HtmlTag');
    $searchForm->addElement($element);
    

    【讨论】:

    • 我刚刚意识到这不符合您在 addElement() 调用中执行此操作的要求,但我认为这是不可能的 - 除非您想创建自己的 Zend_Form 子类,然后你可以做你需要做的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 2011-05-20
    • 1970-01-01
    • 2011-01-10
    • 2011-11-26
    • 1970-01-01
    相关资源
    最近更新 更多