Easy input inside label in ZF2
In my day to day integration work, I need to render ZF2 forms. And the default render for a checkbox formElement was label after checkbox. So you can't click the label to select the checkbox ... WTF ?!
Here is how I deal with this :
<?= $this->formlabel()->openTag($form->get('acceptTerms')) ?>
<?= $this->formcheckbox($form->get('acceptTerms')) ?>
<?= $this->translate($form->get('acceptTerms')->getLabel()) ?>
<?= $this->formlabel()->closeTag($form->get('acceptTerms')) ?>
Let's make clear, we can also extend the default 'formElement' and change it according our needs but, for a front end integrator, it's a nice way to deal with standard components.
Let's see what we're doing :
<?= $this->formlabel()->openTag($form->get('acceptTerms')) ?>
Here we open the <label>
tag
<?= $this->formcheckbox($form->get('acceptTerms')) ?>
Here we render the checkbox element
<?= $this->translate($form->get('acceptTerms')->getLabel()) ?>
and the label text with the translation
<?= $this->formlabel()->closeTag($form->get('acceptTerms')) ?>
and we close the <label>
tag.
That's it :)
Written by Thibault Milan
Related protips
2 Responses
That's what the formRow
view helper is for! ;)
Mind that it wraps the label around the input only as long as you don't specify any id
attribute on the form element.
yep, we use formRow
now and it's awesome .