Question

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?

Was it helpful?

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

OTHER TIPS

I know I'm late to the party, but if you're looking to hide it using jQuery, you can just select based off of the for attribute like so:

$('label[for="password"]').hide();

Should do the trick. Depending on the version of jQuery you may or may not need an @ before the attribute like so:

$('label[@for="password"]').hide();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top