I know I can remove the extra stuff from each element individually like so
$button ->removeDecorator('DtDdWrapper')
->removeDecorator('HtmlTag')
->removeDecorator('Label');
I was wondering if I can achieve the same for all my elements in a zend form?
And how does one remove the dl wrapping the form?
The following 4 lines of code work for me
Lovely
Markus, here is a solution that I use that seems to work well, hopefully it will be suitable for you.
First, in order to render the form with no
<dl>
tag, we need to set the decorators on form object itself. From inside a class extending Zend_Form, you would callZend_Form->setDecorators()
passing an array of form decorators.From the reference guide:
The default decorators for Zend_Form are FormElements, HtmlTag (wraps in a definition list), and Form; the equivalent code for creating them is as follows:
To wrap the form in something other than a dl, we use the above decorators but change the dl to whatever tag you use, I typically use a
div
of classform
which we will see later.Next, the elements need to be dealt with. Zend_Form elements have different decorators for different types of elements. The following groups of element types each have their own distinct set of decorators: [Submit & Button], [Captcha], [File], [Image], and [Radio*]. The decorator for radio is very similar to standard elements except that it does not specify the
for
attribute inside the label.All other form elements, text, password, select, checkbox, etc use the same set of default decorators.
To remove the dd/dt tags from an individual form element we would need to apply our own set of decorators to it. Here is an example that does not use dd/dt tags:
This will wrap each form element in a div tag with the class
form-div
. The problem is, you have to apply this set of decorators to EVERY element that you don't want to be wrapped in the dd/dt tags which can be a bit problematic.To solve this issue, I create a class that extends from Zend_Form and give it some default behavior and decorators that are different from the default decorators for Zend_Form.
While we can't quite have Zend_Form automatically assign the correct decorators to specific element types (you can assign them to specific element names), we can set a default, and give ourselves easy access to the decorators from one place, so if they need to change, they can be easily changed for all forms.
Here is the base class:
Now to use the class, extend all of your forms from this base class and go about assigning elements as usual. If you use
Zend_Form_Element_XXX
as opposed toaddElement()
then you will need to pass one of the decorators as an option to the element constructor, if you use Zend_Form->addElement, then it will use the default decorator$elementDecorators
we assigned in the class.Here is an example that shows how to extend from that class:
This shows a pretty effective way to get rid of the dd/dt and dl elements and replace them with your own. It is a bit inconvenient to have to specify the decorators for every element, as opposed to being able to assign decorators to specific elements, but this seems to work well.
To add one more solution that I think you were looking to do, if you would like to render an element with no label, simply create a new decorator and omit the label decorator from it like this:
Feel free to ask for clarification on anything, hopefully this will help you out.
I think the only way of doing this is to extend Zend_Form and then overide the loadDefaultDecorators() and render() functions as follows. See if this works for you.
Edit:
I think that this method will still be slightly inconvenient as the new render() function will strip any tags you have added to your elements. In order to get around this you would need to extend Zend_Form_Element and overide its loadDefaultDecorators() method in the same way as I have done here for the form.
In my opinion, and probably that of many other developers using Zend_Form there should be no tags in the form markup by default other than
<form>
,<input>
and<label>
tags. Anything else can be added by the developer with existing methods.Use this:
Getting a little late in the thread, but it worked for me
You can disable decorators at form level like this.
This will remove the default decorators and sets the decorators in
$decorators
array as the decorators. If you want to selectively remove decorators, you should look into the implementation of this method and create a similar one for removing decorators.If you want to disable certain decorators for all your forms, create a class
Your_Form
that extendsZend_Form
and remove those decorators onYour_Form
and extend all your forms from this class or simply create instances of this class.