HumHub Documentation (unofficial)

Html extends BaseHtml
in package

Html is an enhanced version of [[\yii\helpers\Html]] helper class dedicated to the Bootstrap needs.

This class inherits all functionality available at [[\yii\helpers\Html]] and can be used as substitute.

Attention: do not confuse [[\yii\bootstrap\Html]] and [[\yii\helpers\Html]], be careful in which class you are using inside your views.

Tags
author

Paul Klimov klimov.paul@gmail.com

since
2.0.5

Table of Contents

Properties

$attributeOrder  : array<string|int, mixed>
$attributeRegex  : string
$dataAttributes  : array<string|int, mixed>
$normalizeClassAttribute  : bool
$voidElements  : array<string|int, mixed>

Methods

a()  : string
Generates a hyperlink tag.
activeCheckbox()  : string
Generates a checkbox tag together with a label for the given model attribute.
activeCheckboxList()  : string
Generates a list of checkboxes.
activeDropDownList()  : string
Generates a drop-down list for the given model attribute.
activeFileInput()  : string
Generates a file input tag for the given model attribute.
activeHiddenInput()  : string
Generates a hidden input tag for the given model attribute.
activeHint()  : string
Generates a hint tag for the given model attribute.
activeInput()  : string
Generates an input tag for the given model attribute.
activeLabel()  : string
Generates a label tag for the given model attribute.
activeListBox()  : string
Generates a list box.
activePasswordInput()  : string
Generates a password input tag for the given model attribute.
activeRadio()  : string
Generates a radio button tag together with a label for the given model attribute.
activeRadioList()  : string
Generates a list of radio buttons.
activeStaticControl()  : string
Generates a Bootstrap static form control for the given model attribute.
activeTextarea()  : string
Generates a textarea tag for the given model attribute.
activeTextInput()  : string
Generates a text input tag for the given model attribute.
addCssClass()  : mixed
Adds a CSS class (or several classes) to the specified options.
addCssStyle()  : mixed
Adds the specified CSS style to the HTML options.
beginForm()  : string
Generates a form start tag.
beginTag()  : string
Generates a start tag.
button()  : string
Generates a button tag.
buttonInput()  : string
Generates an input button.
checkbox()  : string
Generates a checkbox input.
checkboxList()  : string
Generates a list of checkboxes.
csrfMetaTags()  : string
Generates the meta tags containing CSRF token information.
cssFile()  : string
Generates a link tag that refers to an external CSS file.
cssStyleFromArray()  : string
Converts a CSS style array into a string representation.
cssStyleToArray()  : array<string|int, mixed>
Converts a CSS style string into an array representation.
decode()  : string
Decodes special HTML entities back to the corresponding characters.
dropDownList()  : string
Generates a drop-down list.
encode()  : string
Encodes special characters into HTML entities.
endForm()  : string
Generates a form end tag.
endTag()  : string
Generates an end tag.
error()  : string
Generates a tag that contains the first validation error of the specified model attribute.
errorSummary()  : string
Generates a summary of the validation errors.
escapeJsRegularExpression()  : string
Escapes regular expression to use in JavaScript.
fileInput()  : string
Generates a file input field.
getAttributeName()  : string
Returns the real attribute name from the given attribute expression.
getAttributeValue()  : string|array<string|int, mixed>
Returns the value of the specified attribute name or expression.
getInputId()  : string
Generates an appropriate input ID for the specified attribute name or expression.
getInputIdByName()  : string
Converts input name to ID.
getInputName()  : string
Generates an appropriate input name for the specified attribute name or expression.
hiddenInput()  : string
Generates a hidden input field.
icon()  : string
Composes icon HTML for bootstrap Glyphicons.
img()  : string
Generates an image tag.
input()  : string
Generates an input type of the given type.
jsFile()  : string
Generates a script tag that refers to an external JavaScript file.
label()  : string
Generates a label tag.
listBox()  : string
Generates a list box.
mailto()  : string
Generates a mailto hyperlink.
ol()  : string
Generates an ordered list.
passwordInput()  : string
Generates a password input field.
radio()  : string
Generates a radio button input.
radioList()  : string
Generates a list of radio buttons.
removeCssClass()  : mixed
Removes a CSS class from the specified options.
removeCssStyle()  : mixed
Removes the specified CSS style from the HTML options.
renderSelectOptions()  : string
Renders the option tags that can be used by [[dropDownList()]] and [[listBox()]].
renderTagAttributes()  : string
Renders the HTML tag attributes.
resetButton()  : string
Generates a reset button tag.
resetInput()  : string
Generates a reset input button.
script()  : string
Generates a script tag.
staticControl()  : string
Renders Bootstrap static form control.
style()  : string
Generates a style tag.
submitButton()  : string
Generates a submit button tag.
submitInput()  : string
Generates a submit input button.
tag()  : string
Generates a complete HTML tag.
textarea()  : string
Generates a text area input.
textInput()  : string
Generates a text input field.
ul()  : string
Generates an unordered list.
activeBooleanInput()  : string
Generates a boolean input This method is mainly called by [[activeCheckbox()]] and [[activeRadio()]].
activeListInput()  : string
Generates a list of input fields.
booleanInput()  : string
Generates a boolean input.
setActivePlaceholder()  : mixed
Generate placeholder from model attribute label.
collectErrors()  : array<string|int, mixed>
Return array of the validation errors
mergeCssClasses()  : array<string|int, mixed>
Merges already existing CSS classes with new one.
normalizeMaxLength()  : mixed
If `maxlength` option is set true and the model attribute is validated by a string validator, the `maxlength` option will take the max value of [[\yii\validators\StringValidator::max]] and [[\yii\validators\StringValidator::length]].
wrapIntoCondition()  : string
Wraps given content into conditional comments for IE, e.g., `lt IE 9`.

Properties

$attributeOrder

public static array<string|int, mixed> $attributeOrder = ['type', 'id', 'class', 'name', 'value', 'href', 'src', 'srcset', 'form', 'action', 'method', 'selected', 'checked', 'readonly', 'disabled', 'multiple', 'size', 'maxlength', 'width', 'height', 'rows', 'cols', 'alt', 'title', 'rel', 'media']

the preferred order of attributes in a tag. This mainly affects the order of the attributes that are rendered by [[renderTagAttributes()]].

$attributeRegex

public static string $attributeRegex = '/(^|.*\])([\w\.\+]+)(\[.*|$)/u'

Regular expression used for attribute name validation.

Tags
since
2.0.12

$dataAttributes

public static array<string|int, mixed> $dataAttributes = ['aria', 'data', 'data-ng', 'ng']

list of tag attributes that should be specially handled when their values are of array type. In particular, if the value of the data attribute is ['name' => 'xyz', 'age' => 13], two attributes will be generated instead of one: data-name="xyz" data-age="13".

Tags
since
2.0.3

$normalizeClassAttribute

public static bool $normalizeClassAttribute = false

whether to removes duplicate class names in tag attribute class

Tags
see
mergeCssClasses()
see
renderTagAttributes()
since
2.0.44

$voidElements

public static array<string|int, mixed> $voidElements = ['area' => 1, 'base' => 1, 'br' => 1, 'col' => 1, 'command' => 1, 'embed' => 1, 'hr' => 1, 'img' => 1, 'input' => 1, 'keygen' => 1, 'link' => 1, 'meta' => 1, 'param' => 1, 'source' => 1, 'track' => 1, 'wbr' => 1]

list of void elements (element name => 1)

Tags
see
https://html.spec.whatwg.org/multipage/syntax.html#void-element

Methods

a()

Generates a hyperlink tag.

public static a(string $text[, array<string|int, mixed>|string|null $url = null ][, array<string|int, mixed> $options = [] ]) : string
Parameters
$text : string

link body. It will NOT be HTML-encoded. Therefore you can pass in HTML code such as an image tag. If this is coming from end users, you should consider [[encode()]] it to prevent XSS attacks.

$url : array<string|int, mixed>|string|null = null

the URL for the hyperlink tag. This parameter will be processed by [[Url::to()]] and will be used for the "href" attribute of the tag. If this parameter is null, the "href" attribute will not be generated.

If you want to use an absolute url you can call [[Url::to()]] yourself, before passing the URL to this method, like this:

Html::a('link text', Url::to($url, true))
$options : array<string|int, mixed> = []

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.

Tags
see
Url::to()
Return values
string

the generated hyperlink

activeCheckbox()

Generates a checkbox tag together with a label for the given model attribute.

public static activeCheckbox(Model $model, string $attribute[, array<string|int, mixed> $options = [] ]) : string

This method will generate the "checked" tag attribute according to the model attribute value.

Parameters
$model : Model

the model object

$attribute : string

the attribute name or expression. See [[getAttributeName()]] for the format about attribute expression.

$options : array<string|int, mixed> = []

the tag options in terms of name-value pairs. See [[booleanInput()]] for details about accepted attributes.

Return values
string

the generated checkbox tag

activeCheckboxList()

Generates a list of checkboxes.

public static activeCheckboxList(Model $model, string $attribute, array<string|int, mixed> $items[, array<string|int, mixed> $options = [] ]) : string

A checkbox list allows multiple selection, like [[listBox()]]. As a result, the corresponding submitted value is an array. The selection of the checkbox list is taken from the value of the model attribute.

Parameters
$model : Model

the model object

$attribute : string

the attribute name or expression. See [[getAttributeName()]] for the format about attribute expression.

$items : array<string|int, mixed>

the data item used to generate the checkboxes. The array keys are the checkbox values, and the array values are the corresponding labels.

$options : array<string|int, mixed> = []

options (name => config) for the checkbox list container tag. The following options are specially handled:

  • tag: string|false, the tag name of the container element. False to render checkbox without container. See also [[tag()]].

  • unselect: string, the value that should be submitted when none of the checkboxes is selected. You may set this option to be null to prevent default value submission. If this option is not set, an empty string will be submitted.

  • encode: boolean, whether to HTML-encode the checkbox labels. Defaults to true. This option is ignored if item option is set.

  • separator: string, the HTML code that separates items.

  • itemOptions: array, the options for generating the checkbox tag using [[checkbox()]].

  • item: callable, a callback that can be used to customize the generation of the HTML code corresponding to a single item in $items. The signature of this callback must be:

    function ($index, $label, $name, $checked, $value)
    

    where $index is the zero-based index of the checkbox in the whole list; $label is the label for the checkbox; and $name, $value and $checked represent the name, value and the checked status of the checkbox input.

See [[renderTagAttributes()]] for details on how attributes are being rendered.

Return values
string

the generated checkbox list

activeDropDownList()

Generates a drop-down list for the given model attribute.

public static activeDropDownList(Model $model, string $attribute, array<string|int, mixed> $items[, array<string|int, mixed> $options = [] ]) : string

The selection of the drop-down list is taken from the value of the model attribute.

Parameters
$model : Model

the model object

$attribute : string

the attribute name or expression. See [[getAttributeName()]] for the format about attribute expression.

$items : array<string|int, mixed>

the option data items. The array keys are option values, and the array values are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). For each sub-array, an option group will be generated whose label is the key associated with the sub-array. If you have a list of data models, you may convert them into the format described above using [[\yii\helpers\ArrayHelper::map()]].

Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in the labels will also be HTML-encoded.

$options : array<string|int, mixed> = []

the tag options in terms of name-value pairs. The following options are specially handled:

  • prompt: string, a prompt text to be displayed as the first option. Since version 2.0.11 you can use an array to override the value and to set other tag attributes:

    ['text' => 'Please select', 'options' => ['value' => 'none', 'class' => 'prompt', 'label' => 'Select']],
    
  • options: array, the attributes for the select option tags. The array keys must be valid option values, and the array values are the extra attributes for the corresponding option tags. For example,

    [
        'value1' => ['disabled' => true],
        'value2' => ['label' => 'value 2'],
    ];
    
  • groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options', except that the array keys represent the optgroup labels specified in $items.

  • encodeSpaces: bool, whether to encode spaces in option prompt and option value with &nbsp; character. Defaults to false.

  • encode: bool, whether to encode option prompt and option value characters. Defaults to true. This option is available since 2.0.3.

The rest of the options 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 values
string

the generated drop-down list tag

activeFileInput()

Generates a file input tag for the given model attribute.

public static activeFileInput(Model $model, string $attribute[, array<string|int, mixed> $options = [] ]) : string

This method will generate the "name" and "value" tag attributes automatically for the model attribute unless they are explicitly specified in $options. Additionally, if a separate set of HTML options array is defined inside $options with a key named hiddenOptions, it will be passed to the activeHiddenInput field as its own $options parameter.

Parameters
$model : Model

the model object

$attribute : string

the attribute name or expression. See [[getAttributeName()]] for the format about attribute expression.

$options : array<string|int, mixed> = []

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()]]. See [[renderTagAttributes()]] for details on how attributes are being rendered. If hiddenOptions parameter which is another set of HTML options array is defined, it will be extracted from $options to be used for the hidden input.

Return values
string

the generated input tag

activeHiddenInput()

Generates a hidden input tag for the given model attribute.

public static activeHiddenInput(Model $model, string $attribute[, array<string|int, mixed> $options = [] ]) : string

This method will generate the "name" and "value" tag attributes automatically for the model attribute unless they are explicitly specified in $options.

Parameters
$model : Model

the model object

$attribute : string

the attribute name or expression. See [[getAttributeName()]] for the format about attribute expression.

$options : array<string|int, mixed> = []

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()]]. See [[renderTagAttributes()]] for details on how attributes are being rendered.

Return values
string

the generated input tag

activeHint()

Generates a hint tag for the given model attribute.

public static activeHint(Model $model, string $attribute[, array<string|int, mixed> $options = [] ]) : string

The hint text is the hint associated with the attribute, obtained via [[Model::getAttributeHint()]]. If no hint content can be obtained, method will return an empty string.

Parameters
$model : Model

the model object

$attribute : string

the attribute name or expression. See [[getAttributeName()]] for the format about attribute expression.

$options : array<string|int, mixed> = []

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. The following options are specially handled:

  • hint: this specifies the hint to be displayed. Note that this will NOT be [[encode()|encoded]]. If this is not set, [[Model::getAttributeHint()]] will be called to get the hint for display (without encoding).

See [[renderTagAttributes()]] for details on how attributes are being rendered.

Tags
since
2.0.4
Return values
string

the generated hint tag

activeInput()

Generates an input tag for the given model attribute.

public static activeInput(string $type, Model $model, string $attribute[, array<string|int, mixed> $options = [] ]) : string

This method will generate the "name" and "value" tag attributes automatically for the model attribute unless they are explicitly specified in $options.

Parameters
$type : string

the input type (e.g. 'text', 'password')

$model : Model

the model object

$attribute : string

the attribute name or expression. See [[getAttributeName()]] for the format about attribute expression.

$options : array<string|int, mixed> = []

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()]]. See [[renderTagAttributes()]] for details on how attributes are being rendered.

Return values
string

the generated input tag

activeLabel()

Generates a label tag for the given model attribute.

public static activeLabel(Model $model, string $attribute[, array<string|int, mixed> $options = [] ]) : string

The label text is the label associated with the attribute, obtained via [[Model::getAttributeLabel()]].

Parameters
$model : Model

the model object

$attribute : string

the attribute name or expression. See [[getAttributeName()]] for the format about attribute expression.

$options : array<string|int, mixed> = []

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. The following options are specially handled:

  • label: this specifies the label to be displayed. Note that this will NOT be [[encode()|encoded]]. If this is not set, [[Model::getAttributeLabel()]] will be called to get the label for display (after encoding).

See [[renderTagAttributes()]] for details on how attributes are being rendered.

Return values
string

the generated label tag

activeListBox()

Generates a list box.

public static activeListBox(Model $model, string $attribute, array<string|int, mixed> $items[, array<string|int, mixed> $options = [] ]) : string

The selection of the list box is taken from the value of the model attribute.

Parameters
$model : Model

the model object

$attribute : string

the attribute name or expression. See [[getAttributeName()]] for the format about attribute expression.

$items : array<string|int, mixed>

the option data items. The array keys are option values, and the array values are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). For each sub-array, an option group will be generated whose label is the key associated with the sub-array. If you have a list of data models, you may convert them into the format described above using [[\yii\helpers\ArrayHelper::map()]].

Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in the labels will also be HTML-encoded.

$options : array<string|int, mixed> = []

the tag options in terms of name-value pairs. The following options are specially handled:

  • prompt: string, a prompt text to be displayed as the first option. Since version 2.0.11 you can use an array to override the value and to set other tag attributes:

    ['text' => 'Please select', 'options' => ['value' => 'none', 'class' => 'prompt', 'label' => 'Select']],
    
  • options: array, the attributes for the select option tags. The array keys must be valid option values, and the array values are the extra attributes for the corresponding option tags. For example,

    [
        'value1' => ['disabled' => true],
        'value2' => ['label' => 'value 2'],
    ];
    
  • groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options', except that the array keys represent the optgroup labels specified in $items.

  • unselect: string, the value that will be submitted when no option is selected. When this attribute is set, a hidden field will be generated so that if no option is selected in multiple mode, we can still obtain the posted unselect value.

  • encodeSpaces: bool, whether to encode spaces in option prompt and option value with &nbsp; character. Defaults to false.

  • encode: bool, whether to encode option prompt and option value characters. Defaults to true. This option is available since 2.0.3.

The rest of the options 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 values
string

the generated list box tag

activePasswordInput()

Generates a password input tag for the given model attribute.

public static activePasswordInput(Model $model, string $attribute[, array<string|int, mixed> $options = [] ]) : string

This method will generate the "name" and "value" tag attributes automatically for the model attribute unless they are explicitly specified in $options.

Parameters
$model : Model

the model object

$attribute : string

the attribute name or expression. See [[getAttributeName()]] for the format about attribute expression.

$options : array<string|int, mixed> = []

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()]]. See [[renderTagAttributes()]] for details on how attributes are being rendered. The following special options are recognized:

  • maxlength: integer|boolean, when maxlength is set true and the model attribute is validated by a string validator, the maxlength option will take the max value of [[\yii\validators\StringValidator::max]] and [[\yii\validators\StringValidator::length]. This is available since version 2.0.6 and improved taking length into account since version 2.0.42.
  • placeholder: string|boolean, when placeholder equals true, the attribute label from the $model will be used as a placeholder (this behavior is available since version 2.0.14).
Return values
string

the generated input tag

activeRadio()

Generates a radio button tag together with a label for the given model attribute.

public static activeRadio(Model $model, string $attribute[, array<string|int, mixed> $options = [] ]) : string

This method will generate the "checked" tag attribute according to the model attribute value.

Parameters
$model : Model

the model object

$attribute : string

the attribute name or expression. See [[getAttributeName()]] for the format about attribute expression.

$options : array<string|int, mixed> = []

the tag options in terms of name-value pairs. See [[booleanInput()]] for details about accepted attributes.

Return values
string

the generated radio button tag

activeRadioList()

Generates a list of radio buttons.

public static activeRadioList(Model $model, string $attribute, array<string|int, mixed> $items[, array<string|int, mixed> $options = [] ]) : string

A radio button list is like a checkbox list, except that it only allows single selection. The selection of the radio buttons is taken from the value of the model attribute.

Parameters
$model : Model

the model object

$attribute : string

the attribute name or expression. See [[getAttributeName()]] for the format about attribute expression.

$items : array<string|int, mixed>

the data item used to generate the radio buttons. The array keys are the radio values, and the array values are the corresponding labels.

$options : array<string|int, mixed> = []

options (name => config) for the radio button list container tag. The following options are specially handled:

  • tag: string|false, the tag name of the container element. False to render radio button without container. See also [[tag()]].

  • unselect: string, the value that should be submitted when none of the radio buttons is selected. You may set this option to be null to prevent default value submission. If this option is not set, an empty string will be submitted.

  • encode: boolean, whether to HTML-encode the checkbox labels. Defaults to true. This option is ignored if item option is set.

  • separator: string, the HTML code that separates items.

  • itemOptions: array, the options for generating the radio button tag using [[radio()]].

  • item: callable, a callback that can be used to customize the generation of the HTML code corresponding to a single item in $items. The signature of this callback must be:

    function ($index, $label, $name, $checked, $value)
    

    where $index is the zero-based index of the radio button in the whole list; $label is the label for the radio button; and $name, $value and $checked represent the name, value and the checked status of the radio button input.

See [[renderTagAttributes()]] for details on how attributes are being rendered.

Return values
string

the generated radio button list

activeStaticControl()

Generates a Bootstrap static form control for the given model attribute.

public static activeStaticControl(Model $model, string $attribute[, array<string|int, mixed> $options = [] ]) : string
Parameters
$model : Model

the model object.

$attribute : string

the attribute name or expression. See [[getAttributeName()]] for the format about attribute expression.

$options : array<string|int, mixed> = []

the tag options in terms of name-value pairs. See [[staticControl()]] for details.

Tags
see
staticControl()
Return values
string

generated HTML

activeTextarea()

Generates a textarea tag for the given model attribute.

public static activeTextarea(Model $model, string $attribute[, array<string|int, mixed> $options = [] ]) : string

The model attribute value will be used as the content in the textarea.

Parameters
$model : Model

the model object

$attribute : string

the attribute name or expression. See [[getAttributeName()]] for the format about attribute expression.

$options : array<string|int, mixed> = []

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()]]. See [[renderTagAttributes()]] for details on how attributes are being rendered. The following special options are recognized:

  • maxlength: integer|boolean, when maxlength is set true and the model attribute is validated by a string validator, the maxlength option will take the max value of [[\yii\validators\StringValidator::max]] and [[\yii\validators\StringValidator::length]. This is available since version 2.0.6 and improved taking length into account since version 2.0.42.
  • placeholder: string|boolean, when placeholder equals true, the attribute label from the $model will be used as a placeholder (this behavior is available since version 2.0.14).
Return values
string

the generated textarea tag

activeTextInput()

Generates a text input tag for the given model attribute.

public static activeTextInput(Model $model, string $attribute[, array<string|int, mixed> $options = [] ]) : string

This method will generate the "name" and "value" tag attributes automatically for the model attribute unless they are explicitly specified in $options.

Parameters
$model : Model

the model object

$attribute : string

the attribute name or expression. See [[getAttributeName()]] for the format about attribute expression.

$options : array<string|int, mixed> = []

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()]]. See [[renderTagAttributes()]] for details on how attributes are being rendered. The following special options are recognized:

  • maxlength: integer|boolean, when maxlength is set true and the model attribute is validated by a string validator, the maxlength option will take the max value of [[\yii\validators\StringValidator::max]] and [[\yii\validators\StringValidator::length]. This is available since version 2.0.3 and improved taking length into account since version 2.0.42.
  • placeholder: string|boolean, when placeholder equals true, the attribute label from the $model will be used as a placeholder (this behavior is available since version 2.0.14).
Return values
string

the generated input tag

addCssClass()

Adds a CSS class (or several classes) to the specified options.

public static addCssClass(array<string|int, mixed> &$options, string|array<string|int, mixed> $class) : mixed

If the CSS class is already in the options, it will not be added again. If class specification at given options is an array, and some class placed there with the named (string) key, overriding of such key will have no effect. For example:

$options = ['class' => ['persistent' => 'initial']];
Html::addCssClass($options, ['persistent' => 'override']);
var_dump($options['class']); // outputs: array('persistent' => 'initial');
Parameters
$options : array<string|int, mixed>

the options to be modified.

$class : string|array<string|int, mixed>

the CSS class(es) to be added

Tags
see
removeCssClass()

addCssStyle()

Adds the specified CSS style to the HTML options.

public static addCssStyle(array<string|int, mixed> &$options, string|array<string|int, mixed> $style[, bool $overwrite = true ]) : mixed

If the options already contain a style element, the new style will be merged with the existing one. If a CSS property exists in both the new and the old styles, the old one may be overwritten if $overwrite is true.

For example,

Html::addCssStyle($options, 'width: 100px; height: 200px');
Parameters
$options : array<string|int, mixed>

the HTML options to be modified.

$style : string|array<string|int, mixed>

the new style string (e.g. 'width: 100px; height: 200px') or array (e.g. ['width' => '100px', 'height' => '200px']).

$overwrite : bool = true

whether to overwrite existing CSS properties if the new style contain them too.

Tags
see
removeCssStyle()
see
cssStyleFromArray()
see
cssStyleToArray()

beginForm()

Generates a form start tag.

public static beginForm([array<string|int, mixed>|string $action = '' ][, string $method = 'post' ][, array<string|int, mixed> $options = [] ]) : string
Parameters
$action : array<string|int, mixed>|string = ''

the form action URL. This parameter will be processed by [[Url::to()]].

$method : string = 'post'

the form submission method, such as "post", "get", "put", "delete" (case-insensitive). Since most browsers only support "post" and "get", if other methods are given, they will be simulated using "post", and a hidden input will be added which contains the actual method type. See [[\yii\web\Request::methodParam]] for more details.

$options : array<string|int, mixed> = []

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.

Special options:

  • csrf: whether to generate the CSRF hidden input. Defaults to true.
Tags
see
endForm()
Return values
string

the generated form start tag.

beginTag()

Generates a start tag.

public static beginTag(string|bool|null $name[, array<string|int, mixed> $options = [] ]) : string
Parameters
$name : string|bool|null

the tag name. If $name is null or false, the corresponding content will be rendered without any tag.

$options : array<string|int, mixed> = []

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.

Tags
see
endTag()
see
tag()
Return values
string

the generated start tag

button()

Generates a button tag.

public static button([string $content = 'Button' ][, array<string|int, mixed> $options = [] ]) : string
Parameters
$content : string = 'Button'

the content enclosed within the button tag. 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 consider [[encode()]] it to prevent XSS attacks.

$options : array<string|int, mixed> = []

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 values
string

the generated button tag

buttonInput()

Generates an input button.

public static buttonInput([string|null $label = 'Button' ][, array<string|int, mixed> $options = [] ]) : string
Parameters
$label : string|null = 'Button'

the value attribute. If it is null, the value attribute will not be generated.

$options : array<string|int, mixed> = []

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 values
string

the generated button tag

checkbox()

Generates a checkbox input.

public static checkbox(string $name[, bool $checked = false ][, array<string|int, mixed> $options = [] ]) : string
Parameters
$name : string

the name attribute.

$checked : bool = false

whether the checkbox should be checked.

$options : array<string|int, mixed> = []

the tag options in terms of name-value pairs. See [[booleanInput()]] for details about accepted attributes.

Return values
string

the generated checkbox tag

checkboxList()

Generates a list of checkboxes.

public static checkboxList(mixed $name[, mixed $selection = null ][, mixed $items = [] ][, mixed $options = [] ]) : string
Parameters
$name : mixed

the name attribute of each checkbox.

$selection : mixed = null

the selected value(s). String for single or array for multiple selection(s).

$items : mixed = []

the data item used to generate the checkboxes. The array keys are the checkbox values, while the array values are the corresponding labels.

$options : mixed = []

options (name => config) for the checkbox list container tag. The following options are specially handled:

  • tag: string|false, the tag name of the container element. False to render checkbox without container. See also [[tag()]].

  • unselect: string, the value that should be submitted when none of the checkboxes is selected. By setting this option, a hidden input will be generated.

  • disabled: boolean, whether the generated by unselect option hidden input should be disabled. Defaults to false. This option is available since version 2.0.16.

  • encode: boolean, whether to HTML-encode the checkbox labels. Defaults to true. This option is ignored if item option is set.

  • strict: boolean, if $selection is an array and this value is true a strict comparison will be performed on $items keys. Defaults to false. This option is available since 2.0.37.

  • separator: string, the HTML code that separates items.

  • itemOptions: array, the options for generating the checkbox tag using [[checkbox()]].

  • item: callable, a callback that can be used to customize the generation of the HTML code corresponding to a single item in $items. The signature of this callback must be:

    function ($index, $label, $name, $checked, $value)
    

    where $index is the zero-based index of the checkbox in the whole list; $label is the label for the checkbox; and $name, $value and $checked represent the name, value and the checked status of the checkbox input, respectively.

See [[renderTagAttributes()]] for details on how attributes are being rendered.

Tags
since
2.0.8
Return values
string

the generated checkbox list

csrfMetaTags()

Generates the meta tags containing CSRF token information.

public static csrfMetaTags() : string
Tags
see
Request::enableCsrfValidation
Return values
string

the generated meta tags

cssFile()

Generates a link tag that refers to an external CSS file.

public static cssFile(array<string|int, mixed>|string $url[, array<string|int, mixed> $options = [] ]) : string
Parameters
$url : array<string|int, mixed>|string

the URL of the external CSS file. This parameter will be processed by [[Url::to()]].

$options : array<string|int, mixed> = []

the tag options in terms of name-value pairs. The following options are specially handled:

  • condition: specifies the conditional comments for IE, e.g., lt IE 9. When this is specified, the generated link tag will be enclosed within the conditional comments. This is mainly useful for supporting old versions of IE browsers.
  • noscript: if set to true, link tag will be wrapped into <noscript> tags.

The rest of the options will be rendered as the attributes of the resulting link 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.

Tags
see
Url::to()
Return values
string

the generated link tag

cssStyleFromArray()

Converts a CSS style array into a string representation.

public static cssStyleFromArray(array<string|int, mixed> $style) : string

For example,

print_r(Html::cssStyleFromArray(['width' => '100px', 'height' => '200px']));
// will display: 'width: 100px; height: 200px;'
Parameters
$style : array<string|int, mixed>

the CSS style array. The array keys are the CSS property names, and the array values are the corresponding CSS property values.

Return values
string

the CSS style string. If the CSS style is empty, a null will be returned.

cssStyleToArray()

Converts a CSS style string into an array representation.

public static cssStyleToArray(string $style) : array<string|int, mixed>

The array keys are the CSS property names, and the array values are the corresponding CSS property values.

For example,

print_r(Html::cssStyleToArray('width: 100px; height: 200px;'));
// will display: ['width' => '100px', 'height' => '200px']
Parameters
$style : string

the CSS style string

Return values
array<string|int, mixed>

the array representation of the CSS style

dropDownList()

Generates a drop-down list.

public static dropDownList(string $name[, string|bool|array<string|int, mixed>|null $selection = null ][, array<string|int, mixed> $items = [] ][, array<string|int, mixed> $options = [] ]) : string
Parameters
$name : string

the input name

$selection : string|bool|array<string|int, mixed>|null = null

the selected value(s). String/boolean for single or array for multiple selection(s).

$items : array<string|int, mixed> = []

the option data items. The array keys are option values, and the array values are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). For each sub-array, an option group will be generated whose label is the key associated with the sub-array. If you have a list of data models, you may convert them into the format described above using [[\yii\helpers\ArrayHelper::map()]].

Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in the labels will also be HTML-encoded.

$options : array<string|int, mixed> = []

the tag options in terms of name-value pairs. The following options are specially handled:

  • prompt: string, a prompt text to be displayed as the first option. Since version 2.0.11 you can use an array to override the value and to set other tag attributes:

    ['text' => 'Please select', 'options' => ['value' => 'none', 'class' => 'prompt', 'label' => 'Select']],
    
  • options: array, the attributes for the select option tags. The array keys must be valid option values, and the array values are the extra attributes for the corresponding option tags. For example,

    [
        'value1' => ['disabled' => true],
        'value2' => ['label' => 'value 2'],
    ];
    
  • groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options', except that the array keys represent the optgroup labels specified in $items.

  • encodeSpaces: bool, whether to encode spaces in option prompt and option value with &nbsp; character. Defaults to false.

  • encode: bool, whether to encode option prompt and option value characters. Defaults to true. This option is available since 2.0.3.

  • strict: boolean, if $selection is an array and this value is true a strict comparison will be performed on $items keys. Defaults to false. This option is available since 2.0.37.

The rest of the options 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 values
string

the generated drop-down list tag

encode()

Encodes special characters into HTML entities.

public static encode(string $content[, bool $doubleEncode = true ]) : string

The [[\yii\base\Application::charset|application charset]] will be used for encoding.

Parameters
$content : string

the content to be encoded

$doubleEncode : bool = true

whether to encode HTML entities in $content. If false, HTML entities in $content will not be further encoded.

Tags
see
decode()
see
https://www.php.net/manual/en/function.htmlspecialchars.php
Return values
string

the encoded content

endForm()

Generates a form end tag.

public static endForm() : string
Tags
see
beginForm()
Return values
string

the generated tag

endTag()

Generates an end tag.

public static endTag(string|bool|null $name) : string
Parameters
$name : string|bool|null

the tag name. If $name is null or false, the corresponding content will be rendered without any tag.

Tags
see
beginTag()
see
tag()
Return values
string

the generated end tag

error()

Generates a tag that contains the first validation error of the specified model attribute.

public static error(mixed $model, mixed $attribute[, mixed $options = [] ]) : string
Parameters
$model : mixed

the model object

$attribute : mixed

the attribute name or expression. See [[getAttributeName()]] for the format about attribute expression.

$options : mixed = []

the tag options in terms of name-value pairs. The values will be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered.

The following options are specially handled:

  • tag: this specifies the tag name. If not set, "div" will be used. See also [[tag()]].
  • encode: boolean, if set to false then the error message won't be encoded.
  • errorSource (since 2.0.14): \Closure|callable, callback that will be called to obtain an error message. The signature of the callback must be: function ($model, $attribute) and return a string. When not set, the $model->getFirstError() method will be called.

See [[renderTagAttributes()]] for details on how attributes are being rendered.

Tags
since
2.0.8
Return values
string

the generated label tag

errorSummary()

Generates a summary of the validation errors.

public static errorSummary(Model|array<string|int, Model$models[, array<string|int, mixed> $options = [] ]) : string

If there is no validation error, an empty error summary markup will still be generated, but it will be hidden.

Parameters
$models : Model|array<string|int, Model>

the model(s) whose validation errors are to be displayed.

$options : array<string|int, mixed> = []

the tag options in terms of name-value pairs. The following options are specially handled:

  • header: string, the header HTML for the error summary. If not set, a default prompt string will be used.
  • footer: string, the footer HTML for the error summary. Defaults to empty string.
  • encode: boolean, if set to false then the error messages won't be encoded. Defaults to true.
  • showAllErrors: boolean, if set to true every error message for each attribute will be shown otherwise only the first error message for each attribute will be shown. Defaults to false. Option is available since 2.0.10.
  • emptyClass: string, the class name that is added to an empty summary.

The rest of the options will be rendered as the attributes of the container tag.

Return values
string

the generated error summary

escapeJsRegularExpression()

Escapes regular expression to use in JavaScript.

public static escapeJsRegularExpression(string $regexp) : string
Parameters
$regexp : string

the regular expression to be escaped.

Tags
since
2.0.6
Return values
string

the escaped result.

fileInput()

Generates a file input field.

public static fileInput(string $name[, string|null $value = null ][, array<string|int, mixed> $options = [] ]) : string

To use a file input field, you should set the enclosing form's "enctype" attribute to be "multipart/form-data". After the form is submitted, the uploaded file information can be obtained via $_FILES[$name] (see PHP documentation).

Parameters
$name : string

the name attribute.

$value : string|null = null

the value attribute. If it is null, the value attribute will not be generated.

$options : array<string|int, mixed> = []

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 values
string

the generated file input tag

getAttributeName()

Returns the real attribute name from the given attribute expression.

public static getAttributeName(string $attribute) : string

An attribute expression is an attribute name prefixed and/or suffixed with array indexes. It is mainly used in tabular data input and/or input of array type. Below are some examples:

  • [0]content is used in tabular data input to represent the "content" attribute for the first model in tabular input;
  • dates[0] represents the first array element of the "dates" attribute;
  • [0]dates[0] represents the first array element of the "dates" attribute for the first model in tabular input.

If $attribute has neither prefix nor suffix, it will be returned back without change.

Parameters
$attribute : string

the attribute name or expression

Tags
throws
InvalidArgumentException

if the attribute name contains non-word characters.

Return values
string

the attribute name without prefix and suffix.

getAttributeValue()

Returns the value of the specified attribute name or expression.

public static getAttributeValue(Model $model, string $attribute) : string|array<string|int, mixed>

For an attribute expression like [0]dates[0], this method will return the value of $model->dates[0]. See [[getAttributeName()]] for more details about attribute expression.

If an attribute value is an instance of [[ActiveRecordInterface]] or an array of such instances, the primary value(s) of the AR instance(s) will be returned instead.

Parameters
$model : Model

the model object

$attribute : string

the attribute name or expression

Tags
throws
InvalidArgumentException

if the attribute name contains non-word characters.

Return values
string|array<string|int, mixed>

the corresponding attribute value

getInputId()

Generates an appropriate input ID for the specified attribute name or expression.

public static getInputId(Model $model, string $attribute) : string
Parameters
$model : Model

the model object

$attribute : string

the attribute name or expression. See [[getAttributeName()]] for explanation of attribute expression.

Tags
throws
InvalidArgumentException

if the attribute name contains non-word characters.

Return values
string

the generated input ID.

getInputIdByName()

Converts input name to ID.

public static getInputIdByName(string $name) : string

For example, if $name is Post[content], this method will return post-content.

Parameters
$name : string

the input name

Tags
since
2.0.43
Return values
string

the generated input ID

getInputName()

Generates an appropriate input name for the specified attribute name or expression.

public static getInputName(Model $model, string $attribute) : string

This method generates a name that can be used as the input name to collect user input for the specified attribute. The name is generated according to the [[Model::formName|form name]] of the model and the given attribute name. For example, if the form name of the Post model is Post, then the input name generated for the content attribute would be Post[content].

See [[getAttributeName()]] for explanation of attribute expression.

Parameters
$model : Model

the model object

$attribute : string

the attribute name or expression

Tags
throws
InvalidArgumentException

if the attribute name contains non-word characters.

Return values
string

the generated input name

hiddenInput()

Generates a hidden input field.

public static hiddenInput(string $name[, string|null $value = null ][, array<string|int, mixed> $options = [] ]) : string
Parameters
$name : string

the name attribute.

$value : string|null = null

the value attribute. If it is null, the value attribute will not be generated.

$options : array<string|int, mixed> = []

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 values
string

the generated hidden input tag

icon()

Composes icon HTML for bootstrap Glyphicons.

public static icon(string $name[, array<string|int, mixed> $options = [] ]) : string
Parameters
$name : string

icon short name, for example: 'star'

$options : array<string|int, mixed> = []

the tag options in terms of name-value pairs. These will be rendered as the attributes of the resulting tag. There are also a special options:

  • tag: string, tag to be rendered, by default 'span' is used.
  • prefix: string, prefix which should be used to compose tag class, by default 'glyphicon glyphicon-' is used.
Tags
see
http://getbootstrap.com/components/#glyphicons
Return values
string

icon HTML.

img()

Generates an image tag.

public static img(array<string|int, mixed>|string $src[, array<string|int, mixed> $options = [] ]) : string
Parameters
$src : array<string|int, mixed>|string

the image URL. This parameter will be processed by [[Url::to()]].

$options : array<string|int, mixed> = []

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.

Since version 2.0.12 It is possible to pass the srcset option as an array which keys are descriptors and values are URLs. All URLs will be processed by [[Url::to()]].

Return values
string

the generated image tag.

input()

Generates an input type of the given type.

public static input(string $type[, string|null $name = null ][, string|null $value = null ][, array<string|int, mixed> $options = [] ]) : string
Parameters
$type : string

the type attribute.

$name : string|null = null

the name attribute. If it is null, the name attribute will not be generated.

$value : string|null = null

the value attribute. If it is null, the value attribute will not be generated.

$options : array<string|int, mixed> = []

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 values
string

the generated input tag

jsFile()

Generates a script tag that refers to an external JavaScript file.

public static jsFile(string $url[, array<string|int, mixed> $options = [] ]) : string
Parameters
$url : string

the URL of the external JavaScript file. This parameter will be processed by [[Url::to()]].

$options : array<string|int, mixed> = []

the tag options in terms of name-value pairs. The following option is specially handled:

  • condition: specifies the conditional comments for IE, e.g., lt IE 9. When this is specified, the generated script tag will be enclosed within the conditional comments. This is mainly useful for supporting old versions of IE browsers.

The rest of the options will be rendered as the attributes of the resulting script 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.

Tags
see
Url::to()
Return values
string

the generated script tag

label()

Generates a label tag.

public static label(string $content[, string|null $for = null ][, array<string|int, mixed> $options = [] ]) : string
Parameters
$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|null = null

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<string|int, mixed> = []

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 values
string

the generated label tag

listBox()

Generates a list box.

public static listBox(string $name[, string|bool|array<string|int, mixed>|null $selection = null ][, array<string|int, mixed> $items = [] ][, array<string|int, mixed> $options = [] ]) : string
Parameters
$name : string

the input name

$selection : string|bool|array<string|int, mixed>|null = null

the selected value(s). String for single or array for multiple selection(s).

$items : array<string|int, mixed> = []

the option data items. The array keys are option values, and the array values are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). For each sub-array, an option group will be generated whose label is the key associated with the sub-array. If you have a list of data models, you may convert them into the format described above using [[\yii\helpers\ArrayHelper::map()]].

Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in the labels will also be HTML-encoded.

$options : array<string|int, mixed> = []

the tag options in terms of name-value pairs. The following options are specially handled:

  • prompt: string, a prompt text to be displayed as the first option. Since version 2.0.11 you can use an array to override the value and to set other tag attributes:

    ['text' => 'Please select', 'options' => ['value' => 'none', 'class' => 'prompt', 'label' => 'Select']],
    
  • options: array, the attributes for the select option tags. The array keys must be valid option values, and the array values are the extra attributes for the corresponding option tags. For example,

    [
        'value1' => ['disabled' => true],
        'value2' => ['label' => 'value 2'],
    ];
    
  • groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options', except that the array keys represent the optgroup labels specified in $items.

  • unselect: string, the value that will be submitted when no option is selected. When this attribute is set, a hidden field will be generated so that if no option is selected in multiple mode, we can still obtain the posted unselect value.

  • encodeSpaces: bool, whether to encode spaces in option prompt and option value with &nbsp; character. Defaults to false.

  • encode: bool, whether to encode option prompt and option value characters. Defaults to true. This option is available since 2.0.3.

  • strict: boolean, if $selection is an array and this value is true a strict comparison will be performed on $items keys. Defaults to false. This option is available since 2.0.37.

The rest of the options 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 values
string

the generated list box tag

mailto()

Generates a mailto hyperlink.

public static mailto(string $text[, string|null $email = null ][, array<string|int, mixed> $options = [] ]) : string
Parameters
$text : string

link body. It will NOT be HTML-encoded. Therefore you can pass in HTML code such as an image tag. If this is coming from end users, you should consider [[encode()]] it to prevent XSS attacks.

$email : string|null = null

email address. If this is null, the first parameter (link body) will be treated as the email address and used.

$options : array<string|int, mixed> = []

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 values
string

the generated mailto link

ol()

Generates an ordered list.

public static ol(array<string|int, mixed>|Traversable $items[, array<string|int, mixed> $options = [] ]) : string
Parameters
$items : array<string|int, mixed>|Traversable

the items for generating the list. Each item generates a single list item. Note that items will be automatically HTML encoded if $options['encode'] is not set or true.

$options : array<string|int, mixed> = []

options (name => config) for the radio button list. The following options are supported:

  • encode: boolean, whether to HTML-encode the items. Defaults to true. This option is ignored if the item option is specified.

  • itemOptions: array, the HTML attributes for the li tags. This option is ignored if the item option is specified.

  • item: callable, a callback that is used to generate each individual list item. The signature of this callback must be:

    function ($item, $index)
    

    where $index is the array key corresponding to $item in $items. The callback should return the whole list item tag.

See [[renderTagAttributes()]] for details on how attributes are being rendered.

Return values
string

the generated ordered list. An empty string is returned if $items is empty.

passwordInput()

Generates a password input field.

public static passwordInput(string $name[, string|null $value = null ][, array<string|int, mixed> $options = [] ]) : string
Parameters
$name : string

the name attribute.

$value : string|null = null

the value attribute. If it is null, the value attribute will not be generated.

$options : array<string|int, mixed> = []

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 values
string

the generated password input tag

radio()

Generates a radio button input.

public static radio(string $name[, bool $checked = false ][, array<string|int, mixed> $options = [] ]) : string
Parameters
$name : string

the name attribute.

$checked : bool = false

whether the radio button should be checked.

$options : array<string|int, mixed> = []

the tag options in terms of name-value pairs. See [[booleanInput()]] for details about accepted attributes.

Return values
string

the generated radio button tag

radioList()

Generates a list of radio buttons.

public static radioList(mixed $name[, mixed $selection = null ][, mixed $items = [] ][, mixed $options = [] ]) : string
Parameters
$name : mixed

the name attribute of each radio button.

$selection : mixed = null

the selected value(s). String for single or array for multiple selection(s).

$items : mixed = []

the data item used to generate the radio buttons. The array keys are the radio button values, while the array values are the corresponding labels.

$options : mixed = []

options (name => config) for the radio button list container tag. The following options are specially handled:

  • tag: string|false, the tag name of the container element. False to render radio buttons without container. See also [[tag()]].

  • unselect: string, the value that should be submitted when none of the radio buttons is selected. By setting this option, a hidden input will be generated.

  • disabled: boolean, whether the generated by unselect option hidden input should be disabled. Defaults to false. This option is available since version 2.0.16.

  • encode: boolean, whether to HTML-encode the checkbox labels. Defaults to true. This option is ignored if item option is set.

  • strict: boolean, if $selection is an array and this value is true a strict comparison will be performed on $items keys. Defaults to false. This option is available since 2.0.37.

  • separator: string, the HTML code that separates items.

  • itemOptions: array, the options for generating the radio button tag using [[radio()]].

  • item: callable, a callback that can be used to customize the generation of the HTML code corresponding to a single item in $items. The signature of this callback must be:

    function ($index, $label, $name, $checked, $value)
    

    where $index is the zero-based index of the radio button in the whole list; $label is the label for the radio button; and $name, $value and $checked represent the name, value and the checked status of the radio button input, respectively.

See [[renderTagAttributes()]] for details on how attributes are being rendered.

Tags
since
2.0.8
Return values
string

the generated radio button list

removeCssClass()

Removes a CSS class from the specified options.

public static removeCssClass(array<string|int, mixed> &$options, string|array<string|int, mixed> $class) : mixed
Parameters
$options : array<string|int, mixed>

the options to be modified.

$class : string|array<string|int, mixed>

the CSS class(es) to be removed

Tags
see
addCssClass()

removeCssStyle()

Removes the specified CSS style from the HTML options.

public static removeCssStyle(array<string|int, mixed> &$options, string|array<string|int, mixed> $properties) : mixed

For example,

Html::removeCssStyle($options, ['width', 'height']);
Parameters
$options : array<string|int, mixed>

the HTML options to be modified.

$properties : string|array<string|int, mixed>

the CSS properties to be removed. You may use a string if you are removing a single property.

Tags
see
addCssStyle()

renderSelectOptions()

Renders the option tags that can be used by [[dropDownList()]] and [[listBox()]].

public static renderSelectOptions(string|array<string|int, mixed>|bool|null $selection, array<string|int, mixed> $items[, array<string|int, mixed> &$tagOptions = [] ]) : string
Parameters
$selection : string|array<string|int, mixed>|bool|null

the selected value(s). String/boolean for single or array for multiple selection(s).

$items : array<string|int, mixed>

the option data items. The array keys are option values, and the array values are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). For each sub-array, an option group will be generated whose label is the key associated with the sub-array. If you have a list of data models, you may convert them into the format described above using [[\yii\helpers\ArrayHelper::map()]].

Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in the labels will also be HTML-encoded.

$tagOptions : array<string|int, mixed> = []

the $options parameter that is passed to the [[dropDownList()]] or [[listBox()]] call. This method will take out these elements, if any: "prompt", "options" and "groups". See more details in [[dropDownList()]] for the explanation of these elements.

Return values
string

the generated list options

renderTagAttributes()

Renders the HTML tag attributes.

public static renderTagAttributes(array<string|int, mixed> $attributes) : string

Attributes whose values are of boolean type will be treated as boolean attributes.

Attributes whose values are null will not be rendered.

The values of attributes will be HTML-encoded using [[encode()]].

aria and data attributes get special handling when they are set to an array value. In these cases, the array will be "expanded" and a list of ARIA/data attributes will be rendered. For example, 'aria' => ['role' => 'checkbox', 'value' => 'true'] would be rendered as aria-role="checkbox" aria-value="true".

If a nested data value is set to an array, it will be JSON-encoded. For example, 'data' => ['params' => ['id' => 1, 'name' => 'yii']] would be rendered as data-params='{"id":1,"name":"yii"}'.

Parameters
$attributes : array<string|int, mixed>

attributes to be rendered. The attribute values will be HTML-encoded using [[encode()]].

Tags
see
addCssClass()
Return values
string

the rendering result. If the attributes are not empty, they will be rendered into a string with a leading white space (so that it can be directly appended to the tag name in a tag). If there is no attribute, an empty string will be returned.

resetButton()

Generates a reset button tag.

public static resetButton([string $content = 'Reset' ][, array<string|int, mixed> $options = [] ]) : string
Parameters
$content : string = 'Reset'

the content enclosed within the button tag. 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 consider [[encode()]] it to prevent XSS attacks.

$options : array<string|int, mixed> = []

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 values
string

the generated reset button tag

resetInput()

Generates a reset input button.

public static resetInput([string|null $label = 'Reset' ][, array<string|int, mixed> $options = [] ]) : string
Parameters
$label : string|null = 'Reset'

the value attribute. If it is null, the value attribute will not be generated.

$options : array<string|int, mixed> = []

the attributes of the button tag. The values will be HTML-encoded using [[encode()]]. Attributes whose value is null will be ignored and not put in the tag returned. See [[renderTagAttributes()]] for details on how attributes are being rendered.

Return values
string

the generated button tag

script()

Generates a script tag.

public static script(string $content[, array<string|int, mixed> $options = [] ]) : string
Parameters
$content : string

the script content

$options : array<string|int, mixed> = []

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 values
string

the generated script tag

staticControl()

Renders Bootstrap static form control.

public static staticControl(string $value[, array<string|int, mixed> $options = [] ]) : string

By default value will be HTML-encoded using [[encode()]], you may control this behavior via 'encode' option.

Parameters
$value : string

static control value.

$options : array<string|int, mixed> = []

the tag options in terms of name-value pairs. These will be rendered as the attributes of the resulting tag. There are also a special options:

  • encode: bool, whether value should be HTML-encoded or not.
Tags
see
http://getbootstrap.com/css/#forms-controls-static
Return values
string

generated HTML

style()

Generates a style tag.

public static style(string $content[, array<string|int, mixed> $options = [] ]) : string
Parameters
$content : string

the style content

$options : array<string|int, mixed> = []

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 values
string

the generated style tag

submitButton()

Generates a submit button tag.

public static submitButton([string $content = 'Submit' ][, array<string|int, mixed> $options = [] ]) : string

Be careful when naming form elements such as submit buttons. According to the jQuery documentation there are some reserved names that can cause conflicts, e.g. submit, length, or method.

Parameters
$content : string = 'Submit'

the content enclosed within the button tag. 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 consider [[encode()]] it to prevent XSS attacks.

$options : array<string|int, mixed> = []

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 values
string

the generated submit button tag

submitInput()

Generates a submit input button.

public static submitInput([string|null $label = 'Submit' ][, array<string|int, mixed> $options = [] ]) : string

Be careful when naming form elements such as submit buttons. According to the jQuery documentation there are some reserved names that can cause conflicts, e.g. submit, length, or method.

Parameters
$label : string|null = 'Submit'

the value attribute. If it is null, the value attribute will not be generated.

$options : array<string|int, mixed> = []

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 values
string

the generated button tag

tag()

Generates a complete HTML tag.

public static tag(string|bool|null $name[, string $content = '' ][, array<string|int, mixed> $options = [] ]) : string
Parameters
$name : string|bool|null

the tag name. If $name is null or false, the corresponding content will be rendered without any tag.

$content : string = ''

the content to be enclosed between the start and end tags. It will not be HTML-encoded. If this is coming from end users, you should consider [[encode()]] it to prevent XSS attacks.

$options : array<string|int, mixed> = []

the HTML tag attributes (HTML 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.

For example when using ['class' => 'my-class', 'target' => '_blank', 'value' => null] it will result in the html attributes rendered like this: class="my-class" target="_blank".

See [[renderTagAttributes()]] for details on how attributes are being rendered.

Tags
see
beginTag()
see
endTag()
Return values
string

the generated HTML tag

textarea()

Generates a text area input.

public static textarea(string $name[, string $value = '' ][, array<string|int, mixed> $options = [] ]) : string
Parameters
$name : string

the input name

$value : string = ''

the input value. Note that it will be encoded using [[encode()]].

$options : array<string|int, mixed> = []

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. The following special options are recognized:

  • doubleEncode: whether to double encode HTML entities in $value. If false, HTML entities in $value will not be further encoded. This option is available since version 2.0.11.
Return values
string

the generated text area tag

textInput()

Generates a text input field.

public static textInput(string $name[, string|null $value = null ][, array<string|int, mixed> $options = [] ]) : string
Parameters
$name : string

the name attribute.

$value : string|null = null

the value attribute. If it is null, the value attribute will not be generated.

$options : array<string|int, mixed> = []

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 values
string

the generated text input tag

ul()

Generates an unordered list.

public static ul(array<string|int, mixed>|Traversable $items[, array<string|int, mixed> $options = [] ]) : string
Parameters
$items : array<string|int, mixed>|Traversable

the items for generating the list. Each item generates a single list item. Note that items will be automatically HTML encoded if $options['encode'] is not set or true.

$options : array<string|int, mixed> = []

options (name => config) for the radio button list. The following options are supported:

  • encode: boolean, whether to HTML-encode the items. Defaults to true. This option is ignored if the item option is specified.

  • separator: string, the HTML code that separates items. Defaults to a simple newline ("\n"). This option is available since version 2.0.7.

  • itemOptions: array, the HTML attributes for the li tags. This option is ignored if the item option is specified.

  • item: callable, a callback that is used to generate each individual list item. The signature of this callback must be:

    function ($item, $index)
    

    where $index is the array key corresponding to $item in $items. The callback should return the whole list item tag.

See [[renderTagAttributes()]] for details on how attributes are being rendered.

Return values
string

the generated unordered list. An empty list tag will be returned if $items is empty.

activeBooleanInput()

Generates a boolean input This method is mainly called by [[activeCheckbox()]] and [[activeRadio()]].

protected static activeBooleanInput(string $type, Model $model, string $attribute[, array<string|int, mixed> $options = [] ]) : string
Parameters
$type : string

the input type. This can be either radio or checkbox.

$model : Model

the model object

$attribute : string

the attribute name or expression. See [[getAttributeName()]] for the format about attribute expression.

$options : array<string|int, mixed> = []

the tag options in terms of name-value pairs. See [[booleanInput()]] for details about accepted attributes.

Tags
since
2.0.9
Return values
string

the generated input element

activeListInput()

Generates a list of input fields.

protected static activeListInput(string $type, Model $model, string $attribute, array<string|int, mixed> $items[, array<string|int, mixed> $options = [] ]) : string

This method is mainly called by [[activeListBox()]], [[activeRadioList()]] and [[activeCheckboxList()]].

Parameters
$type : string

the input type. This can be 'listBox', 'radioList', or 'checkBoxList'.

$model : Model

the model object

$attribute : string

the attribute name or expression. See [[getAttributeName()]] for the format about attribute expression.

$items : array<string|int, mixed>

the data item used to generate the input fields. The array keys are the input values, and the array values are the corresponding labels.

$options : array<string|int, mixed> = []

options (name => config) for the input list. The supported special options depend on the input type specified by $type.

Return values
string

the generated input list

booleanInput()

Generates a boolean input.

protected static booleanInput(string $type, string $name[, bool $checked = false ][, array<string|int, mixed> $options = [] ]) : string
Parameters
$type : string

the input type. This can be either radio or checkbox.

$name : string

the name attribute.

$checked : bool = false

whether the checkbox should be checked.

$options : array<string|int, mixed> = []

the tag options in terms of name-value pairs. The following options are specially handled:

  • uncheck: string, the value associated with the uncheck state of the checkbox. When this attribute is present, a hidden input will be generated so that if the checkbox is not checked and is submitted, the value of this attribute will still be submitted to the server via the hidden input.
  • label: string, a label displayed next to the checkbox. 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. When this option is specified, the checkbox will be enclosed by a label tag.
  • labelOptions: array, the HTML attributes for the label tag. Do not set this option unless you set the "label" option.

The rest of the options will be rendered as the attributes of the resulting checkbox 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.

Tags
since
2.0.9
Return values
string

the generated checkbox tag

setActivePlaceholder()

Generate placeholder from model attribute label.

protected static setActivePlaceholder(Model $model, string $attribute[, array<string|int, mixed> &$options = [] ]) : mixed
Parameters
$model : Model

the model object

$attribute : string

the attribute name or expression. See [[getAttributeName()]] for the format about attribute expression.

$options : array<string|int, mixed> = []

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()]].

Tags
since
2.0.14

collectErrors()

Return array of the validation errors

private static collectErrors(Model|array<string|int, Model$models, mixed $encode, mixed $showAllErrors) : array<string|int, mixed>
Parameters
$models : Model|array<string|int, Model>

the model(s) whose validation errors are to be displayed.

$encode : mixed

boolean, if set to false then the error messages won't be encoded.

$showAllErrors : mixed

boolean, if set to true every error message for each attribute will be shown otherwise only the first error message for each attribute will be shown.

Tags
since
2.0.14
Return values
array<string|int, mixed>

of the validation errors

mergeCssClasses()

Merges already existing CSS classes with new one.

private static mergeCssClasses(array<string|int, mixed> $existingClasses, array<string|int, mixed> $additionalClasses) : array<string|int, mixed>

This method provides the priority for named existing classes over additional.

Parameters
$existingClasses : array<string|int, mixed>

already existing CSS classes.

$additionalClasses : array<string|int, mixed>

CSS classes to be added.

Tags
see
addCssClass()
Return values
array<string|int, mixed>

merge result.

normalizeMaxLength()

If `maxlength` option is set true and the model attribute is validated by a string validator, the `maxlength` option will take the max value of [[\yii\validators\StringValidator::max]] and [[\yii\validators\StringValidator::length]].

private static normalizeMaxLength(Model $model, string $attribute, array<string|int, mixed> &$options) : mixed
Parameters
$model : Model

the model object

$attribute : string

the attribute name or expression.

$options : array<string|int, mixed>

the tag options in terms of name-value pairs.

wrapIntoCondition()

Wraps given content into conditional comments for IE, e.g., `lt IE 9`.

private static wrapIntoCondition(string $content, string $condition) : string
Parameters
$content : string

raw HTML content.

$condition : string

condition string.

Return values
string

generated HTML.


        
On this page

Search results