|
Size: | 17236 |
Storage flags: | no_autoload,compress/gzip (24%) |
The FormLabel view helper is used to render a <label> HTML element and its attributes. If you have a Zend\I18n\Translator\Translator attached, FormLabel will translate the label contents during it’s rendering.
Basic usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | use Zend\Form\Element;
$element = new Element\Text('my-text');
$element->setLabel('Label')
->setAttribute('id', 'text-id')
->setLabelAttributes(array('class' => 'control-label'));
// Within your view...
/**
* Example #1: Render label in one shot
*/
echo $this->formLabel($element);
// <label class="control-label" for="text-id">Label</label>
echo $this->formLabel($element, $this->formText($element));
// <label class="control-label" for="text-id">Label<input type="text" name="my-text"></label>
echo $this->formLabel($element, $this->formText($element), 'append');
// <label class="control-label" for="text-id"><input type="text" name="my-text">Label</label>
/**
* Example #2: Render label in separate steps
*/
// Render the opening tag
echo $this->formLabel()->openTag($element);
// <label class="control-label" for="text-id">
// Render the closing tag
echo $this->formLabel()->closeTag();
// </label>
/**
* Example #3: Render html label after toggling off escape
*/
$element->setLabel('<abbr title="Completely Automated Public Turing test to tell Computers and Humans Apart">CAPTCHA</abbr>');
$element->setLabelOptions(array('disable_html_escape' => true));
echo $this->formLabel($element);
// <label class="control-label" for="text-id">
// <abbr title="Completely Automated Public Turing test to tell Computers and Humans Apart">CAPTCHA</abbr>
// </label>
|
Note
HTML escape only applies to the Element::$label property, not to the helper $labelContent parameter.
Attaching a translator and setting a text domain:
1 2 3 4 5 6 7 8 | // Setting a translator
$this->formLabel()->setTranslator($translator);
// Setting a text domain
$this->formLabel()->setTranslatorTextDomain('my-text-domain');
// Setting both
$this->formLabel()->setTranslator($translator, 'my-text-domain');
|
Note
If you have a translator in the Service Manager under the key, ‘translator’, the view helper plugin manager will automatically attach the translator to the FormLabel view helper. See Zend\View\HelperPluginManager::injectTranslator() for more information.
The following public methods are in addition to those inherited from Zend\Form\View\Helper\AbstractHelper.
Render a form label, optionally with content.
Always generates a “for” statement, as we cannot assume the form input will be provided in the $labelContent.
Parameters: |
|
---|---|
Return type: | string |
Renders the <label> open tag and attributes.
Parameters: | $attributesOrElement – An array of key value attributes or a ElementInterface instance. |
---|---|
Return type: | string |
Renders a </label> closing tag.
Return type: | string |
---|
The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.