label() public static method

Generates a label tag.
public static label ( string $content, string $for = null, array $options = [] ) : string
$content string label text. It will NOT be HTML-encoded. Therefore you can pass in HTML code such as an image tag. If this is is coming from end users, you should [[encode()]] it to prevent XSS attacks.
$for string the ID of the HTML element that this label is associated with. If this is null, the "for" attribute will not be generated.
$options array the tag options in terms of name-value pairs. These will be rendered as the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. See [[renderTagAttributes()]] for details on how attributes are being rendered.
return string the generated label tag
Example #1
0
 /**
  * @inheritDoc
  */
 public static function radio($name, $checked = false, $options = [])
 {
     if (!isset($options['id'])) {
         $options['id'] = Widget::$autoIdPrefix . Widget::$counter++;
     }
     $content = parent::radio($name, $checked, array_merge($options, ['label' => null]));
     if (isset($options['label'])) {
         $label = $options['label'];
         $for = $options['id'];
         $labelOptions = isset($options['labelOptions']) ? $options['labelOptions'] : [];
         unset($options['label'], $options['labelOptions']);
         $content .= parent::label($label, $for, $labelOptions);
     }
     return $content;
 }