Setting a label's id in Zend_Form - zend-framework

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?

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

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();

Related

zend form element add div after input field

my zend form generates following code:
<dt id="register_username-label">
<label for="register_username" class="required">Membername*</label>
</dt>
<dd id="register_username-element">
<input type="text" name="register_username" id="register_username" value="" />
</dd>
but in some cases (handled in a separete decorator) i need to add some more html next to the input field (also in the dd tag). i have a instance of Zend_Form_Element_Text witch i could add some more decorators, but i don't know how to get this done :(
solution should look like this:
<dt id="register_username-label">
<label for="register_username" class="required">Membername*</label>
</dt>
<dd id="register_username-element">
<input type="text" name="register_username" id="register_username" value="" />
<div class="validate"><div class="validate-check"></div></div>
</dd>
You can add decorators to your form element in form file just as below
$form->addElement(
'text',
'register_username',
array(
'required' => false,
'decorators' => array(
array(
'HtmlTag', array(
'tag' => 'div',
'class' => 'validate'
)
)
)
)
);
And more i would like to share with you one interesting Link to understand how zend form is basically works.
Please let me know if i can help you more.

Drupal 7 forms. How do you wrap an input in a label?

I'm developing a custom form module in Drupal 7. I would like to wrap my form inputs in the label tags like this:
<div class="form-item form-type-textfield form-item-FirstName">
<label for="edit-firstname">First Name
<span class="form-required" title="This field is required.">*</span>
<input type="text" id="edit-firstname" name="FirstName" value="" size="25" maxlength="37" class="form-text required" /></label>
</div>
The label closing tag is after the end of the input. Normally it would be after the span closing tag.
I think I'll need to override the 'theme_form_element_label' function in the 'includes/form.inc' but I'm not sure how to go about it.
I don't know why you would wrap a form element around a form element. But to answer your question use the '#prefix' and '#suffix' keys to add your label.
So your code may look similar to this:
$form['first_name'] = array(
'#type' => 'textfield',
'#prefix' => '<label for="edit-firstname">' . t('First Name'),
'#suffix' => '</label>',
'#required' => TRUE
);
If you are trying to put a label next to a textfield all you would need to do is add the '#title' key to your textfield element. So it may look like this:
$form['first_name'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#required' => TRUE
);

How to make a selectbox the label of a radio button in Zend Fw

Not my idea, but I need a set of radio buttons, where the last buttons value is a select box. Visual explanation:
o Opt 1
o Opt 2
o |___SelectBox|
What it would look like in HTML:
<input type="radio" name="radioSet">Opt1
</input>
<input type="radio" name="radioSet">Opt2
</input>
<input type="radio" name="radioSet"><!-- Opt 3 -->
<select>
<option value="a"> aaa</option>
<option value="b"> bbb</option>
</select>
</input>
What I've done in ZF so far:
$picker = new Zend_Form_Element_Select('selectBox', array(
'multiOptions' => array('a'=>'aaa', 'b' =>'bbb'),
'decorators' => array(
array('Label', array('escape'=>false))
)
));
$this->addElement(
'radio',
'radioSet',
array(
'multioptions' => array(
'x'=>'Opt1',
'y'=>'Opt2',
'z'=>$picker //'Dropdown picker'
),
'label' => 'My Title:',
'decorators' => array(
'ViewHelper',
'Errors',
array('Description', array('escape' => false)),
'Label',
array('HtmlTag', array('tag'=>'div')),
),
)
);
But this returns just the 3 radio buttons, as well as the labels "Opt1" and "Opt2", but nothing after the third radio button.
I WANT it to be like the HTML code shown above, but this ZF code does not return it. Anyone an idea how this can be accomplished?
Thanks!
unfortunately you probably going to have to write a decorator to replace the label tag with a select. Looking at the code for the Zend_Form_Element_Radio() it specifically adds the label tag to the radio.
Thanks for your advices. I'm quite new to Zend, so I checked up how to make a custom view helper decorator, I couldn't manage to get it to work like that, but it helped me in another problem though.
I came to the solution, that it's easier to just add the select box as an individual element afterwards, and style it to the desired position with css.
Thanks again.

Zend Framework: Captcha problem

Iam using following code to generate CAPTCHA :
$captcha = $this->createElement('captcha', 'captcha',
array('required' => true,
'captcha' => array('captcha' => 'Image',
'font' => 'resource/fonts/arial.ttf',
'fontSize' => '24',
'wordLen' => '5',
'height' => '50',
'width' => '150',
'imgDir' => 'resource/captcha',
'imgUrl' => 'resource/captcha',
'gcFreq'=>'10',
'dotNoiseLevel' => '10',
'lineNoiseLevel' => '2')));
$captcha->setLabel('Captcha');
Following code is generated:
<label for="captcha-input" class="login_label required">Captcha</label>
<img width="150" height="50" alt="" src="captcha/eb3a592c8b1c7a71b0c7ce5179422be2.png" />
<input type="hidden" name="captcha[id]" value="eb3a592c8b1c7a71b0c7ce5179422be2" id="captcha-id">
<input type="text" name="captcha[input]" id="captcha-input" value="">
<input type="text" name="captcha" id="captcha" value="eb3a592c8b1c7a71b0c7ce5179422be2">
Can someone guide me how can I remove extra input text fields like
<input type="text" name="captcha" id="captcha" value="eb3a592c8b1c7a71b0c7ce5179422be2">
Thanks in advance
It's important to do the
$this->getElement('captcha')->removeDecorator("viewhelper");
after you have enabled the ElementsDecorators (which sets the ViewHelper in the first place - don't delete this, it's required anyway)
For me it looks like this:
$this->setElementDecorators(array(
'ViewHelper',
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td')),
array('Label', array('tag' => 'td')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
));
$this->getElement('captcha')->removeDecorator("viewhelper");
That input is not "extra" - it's essential.
When the CAPTCHA is validated, the value of that field is used to look up the correct solution to the CAPTCHA, which is then compared against the user's input.
Without that field, your CAPTCHA will break.
Why would you want to remove it in the first place?
You can try this:
$this->getElement('captcha')->removeDecorator("viewhelper");
I had the same problem. Remove the "ViewHelper" decorator and the captcha will render properly.

Zend Checkbox Decorator Issue

Hello I am trying to do a checkbox display like:
<input name="choice2" id="choice2" value="1" type="checkbox">
<label for="choice2" class="optional">Credit Card</label><br />
Some text
<input name="choice2" id="choice2" value="1" type="checkbox">
<label for="choice2" class="optional">Credit Card</label><br />
Some text
I got very close with the following code:
$lbl_spagym = 'Credit Card<br />
<p class="description">Some text</p><br />';
$chk_spagym = new Zend_Form_Element_Checkbox('chk_spagym');
$chk_spagym->setLabel($lbl_spagym)
->setDecorators(array(
'ViewHelper',
'Description',
'Errors',
array('Label', array('placement' => 'APPEND', 'escape' => false)),
array('HtmlTag', array('tag' => 'div'))
));
It renders as:
<div>
<input type="hidden" name="chk_bank" value="0" />
<input type="checkbox" name="chk_bank" id="chk_bank" value="1" checked="checked" />
<label for="chk_bank" class="optional">Credit Card<br />
<p class="relocation_descr">Some text</p><br />
</label></div>
But I need the <p></p> to be out of the label tag because 'some text' is a description and not a label. I played with decorators for days but can not get this behavior right. Would thanks any comments on that.
You should be able to create that markup using something like
$form->addElement('checkbox', 'choice2', array(
'label' => 'Credit Card',
'decorators' => array(
'ViewHelper',
array('Label', array('placement' => 'append',
'class' => 'optional'))
)
));
This will generate a hidden element for the checkbox "off" value but trust me, you want to keep that.
I sorted my problem with:
$checkbox_e->setLabel($e_label)
->setDecorators(array('ViewHelper',
'Description',
'Errors',
array('Label',
array('placement'=>'APPEND')),
array('HtmlTag', array('tag' => 'div'))));