views:

1167

answers:

3

Zend automatically adds tags around form elements it has generated. How may I remove these tags as paramaters to the addElement function.

I have tried changing the disableLoadDefaultDecorators flag, however the element does not render at all then.

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

A: 

I think this will work to remove the HtmlTag decorator:

$element = $searchForm->createElement('text', 'searchText', array('class'=>'onClickClear'));
$element->removeDecorator('HtmlTag');
$searchForm->addElement($element);
Brock Boland
I just realized this doesn't meet your request to do it in the `addElement()` call, but I don't think that's possible - unless you want to create your own subclass of Zend_Form, and then you can do what you need to.
Brock Boland
A: 

You can override the default decorators in the createElement / addElement by passing an array of decorators to load.

The "ViewHelper" decorator usually renders the form element itself, "Errors" for problems with validators, and "Label" for your form element are usually handy.

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

Another way is to call setElementDecorators() right after initialising your form, which sets the default decorators for all subsequent elements. I use the code below for very simple (one or two field forms) that I just want displayed on one line and that do not need extensive validation:

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