通过参数删除Zend表单元素上的装饰器

时间:2009-07-06 16:21:32

标签: php zend-framework zend-form

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

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

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

3 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:2)

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

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

答案 2 :(得分:0)

我认为这样可以删除HtmlTag装饰器:

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