Will automatically generate a for attribute if one is not provided.
### Options
- for - Set the for attribute, if its not defined the for attribute
will be generated from the $fieldName parameter using
FormHelper::_domId().
Examples:
The text and for attribute are generated off of the fieldname
echo $this->Form->label('published');
Custom text:
echo $this->Form->label('published', 'Publish');
Custom attributes:
echo $this->Form->label('published', 'Publish', [
'for' => 'post-publish'
]);
Nesting an input tag:
echo $this->Form->label('published', 'Publish', [
'for' => 'published',
'input' => $this->text('published'),
]);
If you want to nest inputs in the labels, you will need to modify the default templates.
public label ( string $fieldName, string | null $text = null, array $options = [] ) : string | ||
$fieldName | string | This should be "modelname.fieldname" |
$text | string | null | Text that will appear in the label field. If $text is left undefined the text will be inflected from the fieldName. |
$options | array | An array of HTML attributes. |
return | string | The formatted LABEL element |
/** * Override * * @param string $fieldName field name * @param string $text caption * @param array $options An array of HTML attributes. * @return string */ public function label($fieldName, $text = null, array $options = []) { $options = $this->addClass($options, 'control-label'); if ($this->_isHorizontal === true) { $options = $this->addClass($options, 'col-sm-2'); } return parent::label($fieldName, $text, $options); }
/** * Returns a formatted `<label>` element. * Will automatically generate a `for` attribute if one is not provided. * @param string $fieldName Field name, should be "Modelname.fieldname" * @param string $text Text that will appear in the label field. If is * left undefined the text will be inflected from the fieldName * @param array|string $options HTML attributes, or a string to be used * as a class name * @return string */ public function label($fieldName, $text = null, array $options = []) { $options = $this->optionsDefaults(['escape' => false], $options); list($text, $options) = $this->addIconToText($text, $options); return parent::label($fieldName, $text, $options); }
public function label($fieldName, $text = null, array $options = []) { $options = $this->addClass($options, 'control-label'); return parent::label($fieldName, $text, $options); }
/** * * Create & return a label message (Twitter Boostrap like). * **/ public function label($fieldName, $text = NULL, array $options = []) { $nolabel = strpos($options['input'], 'type="checkbox"') !== false || strpos($options['input'], 'type="radio"') !== false; if ($nolabel) { return $text; } return parent::label($fieldName, $text, $options); }