Search code examples
zend-frameworkzend-formzend-form-element

Setting a label's id in Zend_Form


I'm trying to set an id for one of my form's labels so I can hide it with jquery later. Here's my element:

$password = new Zend_Form_Element_Password('password');
$password->setLabel('Password:')
    ->setRequired(true)
    ->addFilter('StripTags')
    ->addFilter('StringTrim')
    ->addValidator('NotEmpty')
    ->setAttrib( "id", "password" );

In the source, it looks like this:

<dt>
<label for="password" class="required">Password:</label>
</dt>

<dd>
<input type="password" name="password" id="password" value="">
</dd>

I need the label to look like:

<label id="pass_label" for="password" class="required">Password:</label>

Any ideas?


Solution

  • It doesn't seem possible to change the label's id using decorators, but you can easily add a classname

    $decorators = array(
            array('ViewHelper'),
            array('Errors'),
            array('Description', array('tag' => 'p', 'class' => 'description')),
            array('HtmlTag', array('tag' => 'dd')),
            array('Label', array('tag' => 'dt', 'class' => 'pass_label')),
        );
    
        $password->setDecorators($decorators);
    

    This results in

    <label class="pass_label optional" for="password">Password</label>
    

    I tried to set the id in the same way and this does not work. If you pass an id in as the options array, this changes the value of the 'for' attribute