HumHub Documentation (unofficial)

ActiveField extends ActiveField
in package

A Bootstrap 3 enhanced version of [[\yii\widgets\ActiveField]].

This class adds some useful features to [[\yii\widgets\ActiveField|ActiveField]] to render all sorts of Bootstrap 3 form fields in different form layouts:

  • [[inputTemplate]] is an optional template to render complex inputs, for example input groups
  • [[horizontalCssClasses]] defines the CSS grid classes to add to label, wrapper, error and hint in horizontal forms
  • [[inline]]/[[inline()]] is used to render inline [[checkboxList()]] and [[radioList()]]
  • [[enableError]] can be set to false to disable to the error
  • [[enableLabel]] can be set to false to disable to the label
  • [[label()]] can be used with a bool argument to enable/disable the label

There are also some new placeholders that you can use in the [[template]] configuration:

  • {beginLabel}: the opening label tag
  • {labelTitle}: the label title for use with {beginLabel}/{endLabel}
  • {endLabel}: the closing label tag
  • {beginWrapper}: the opening wrapper tag
  • {endWrapper}: the closing wrapper tag

The wrapper tag is only used for some layouts and form elements.

Note that some elements use slightly different defaults for [[template]] and other options. You may want to override those predefined templates for checkboxes, radio buttons, checkboxLists and radioLists in the [[\yii\widgets\ActiveForm::fieldConfig|fieldConfig]] of the [[\yii\widgets\ActiveForm]]:

  • [[checkboxTemplate]] the template for checkboxes in default layout
  • [[radioTemplate]] the template for radio buttons in default layout
  • [[horizontalCheckboxTemplate]] the template for checkboxes in horizontal layout
  • [[horizontalRadioTemplate]] the template for radio buttons in horizontal layout
  • [[inlineCheckboxListTemplate]] the template for inline checkboxLists
  • [[inlineRadioListTemplate]] the template for inline radioLists

Example:

use yii\bootstrap\ActiveForm;

$form = ActiveForm::begin(['layout' => 'horizontal']);

// Form field without label
echo $form->field($model, 'demo', [
    'inputOptions' => [
        'placeholder' => $model->getAttributeLabel('demo'),
    ],
])->label(false);

// Inline radio list
echo $form->field($model, 'demo')->inline()->radioList($items);

// Control sizing in horizontal mode
echo $form->field($model, 'demo', [
    'horizontalCssClasses' => [
        'wrapper' => 'col-sm-2',
    ]
]);

// With 'default' layout you would use 'template' to size a specific field:
echo $form->field($model, 'demo', [
    'template' => '{label} <div class="row"><div class="col-sm-4">{input}{error}{hint}</div></div>'
]);

// Input group
echo $form->field($model, 'demo', [
    'inputTemplate' => '<div class="input-group"><span class="input-group-addon">@</span>{input}</div>',
]);

ActiveForm::end();
Tags
see
ActiveForm
see
http://getbootstrap.com/css/#forms
author

Michael Härtl haertl.mike@gmail.com

since
2.0

Table of Contents

Properties

$addAriaAttributes  : bool
$attribute  : string
$behaviors  : array<string|int, Behavior>
$checkboxTemplate  : string
$enableAjaxValidation  : bool|null
$enableClientValidation  : bool|null
$enableError  : bool
$enableLabel  : bool
$errorOptions  : array<string|int, mixed>
$form  : ActiveForm
$hintOptions  : array<string|int, mixed>
$horizontalCheckboxTemplate  : string
$horizontalCssClasses  : null|array<string|int, mixed>
$horizontalRadioTemplate  : string
$inline  : bool
$inlineCheckboxListTemplate  : string
$inlineRadioListTemplate  : string
$inputOptions  : array<string|int, mixed>
$inputTemplate  : string|null
$labelOptions  : array<string|int, mixed>
$model  : Model
$options  : array<string|int, mixed>
$parts  : array<string|int, mixed>
$radioTemplate  : string
$selectors  : array<string|int, mixed>
$template  : string
$validateOnBlur  : bool|null
$validateOnChange  : bool|null
$validateOnType  : bool|null
$validationDelay  : int|null
$wrapperOptions  : array<string|int, mixed>
$_behaviors  : array<string|int, Behavior>|null
$_events  : array<string|int, mixed>
$_eventWildcards  : array<string|int, mixed>
$_inputId  : string
$_skipLabelFor  : bool

Methods

__call()  : mixed
Calls the named method which is not a class method.
__clone()  : mixed
This method is called after the object is created by cloning an existing one.
__construct()  : mixed
Constructor.
__get()  : mixed
Returns the value of a component property.
__isset()  : bool
Checks if a property is set, i.e. defined and not null.
__set()  : mixed
Sets the value of a component property.
__toString()  : string
PHP magic method that returns the string representation of this object.
__unset()  : mixed
Sets a component property to be null.
attachBehavior()  : Behavior
Attaches a behavior to this component.
attachBehaviors()  : mixed
Attaches a list of behaviors to the component.
begin()  : string
Renders the opening tag of the field container.
behaviors()  : array<string|int, mixed>
Returns a list of behaviors that this component should behave as.
canGetProperty()  : bool
Returns a value indicating whether a property can be read.
canSetProperty()  : bool
Returns a value indicating whether a property can be set.
checkbox()  : $this
Renders a checkbox.
checkboxList()  : $this
Renders a list of checkboxes.
className()  : string
Returns the fully qualified name of this class.
detachBehavior()  : Behavior|null
Detaches a behavior from the component.
detachBehaviors()  : mixed
Detaches all behaviors from the component.
dropDownList()  : $this
Renders a drop-down list.
end()  : string
Renders the closing tag of the field container.
ensureBehaviors()  : mixed
Makes sure that the behaviors declared in [[behaviors()]] are attached to this component.
error()  : $this
Generates a tag that contains the first validation error of [[attribute]].
fileInput()  : $this
Renders a file input.
getBehavior()  : Behavior|null
Returns the named behavior object.
getBehaviors()  : array<string|int, Behavior>
Returns all behaviors attached to this component.
hasEventHandlers()  : bool
Returns a value indicating whether there is any handler attached to the named event.
hasMethod()  : bool
Returns a value indicating whether a method is defined.
hasProperty()  : bool
Returns a value indicating whether a property is defined for this component.
hiddenInput()  : $this
Renders a hidden input.
hint()  : $this
Renders the hint tag.
init()  : mixed
Initializes the object.
inline()  : $this
input()  : $this
Renders an input tag.
label()  : $this
Generates a label tag for [[attribute]].
listBox()  : $this
Renders a list box.
off()  : bool
Detaches an existing event handler from this component.
on()  : mixed
Attaches an event handler to an event.
passwordInput()  : $this
Renders a password input.
radio()  : $this
Renders a radio button.
radioList()  : $this
Renders a list of radio buttons.
render()  : string
Renders the whole field.
staticControl()  : $this
Renders Bootstrap static form control.
textarea()  : $this
Renders a text area.
textInput()  : $this
Renders a text input.
trigger()  : mixed
Triggers an event.
widget()  : $this
Renders a widget as the input of the field.
addAriaAttributes()  : mixed
Adds aria attributes to the input options.
addErrorClassIfNeeded()  : mixed
Adds validation class to the input options if needed.
addRoleAttributes()  : mixed
Add role attributes to the input options
adjustLabelFor()  : mixed
Adjusts the `for` attribute for the label based on the input options.
createLayoutConfig()  : array<string|int, mixed>
getClientOptions()  : array<string|int, mixed>
Returns the JS options for the field.
getInputId()  : string
Returns the HTML `id` of the input element of this form field.
isAjaxValidationEnabled()  : bool
Checks if ajax validation enabled for the field.
isClientValidationEnabled()  : bool
Checks if client validation enabled for the field.
renderLabelParts()  : mixed
attachBehaviorInternal()  : Behavior
Attaches a behavior to this component.

Properties

$addAriaAttributes

public bool $addAriaAttributes = true

adds aria HTML attributes aria-required and aria-invalid for inputs

Tags
since
2.0.11

$attribute

public string $attribute

the model attribute that this field is associated with.

$behaviors read-only

public array<string|int, Behavior> $behaviors

List of behaviors attached to this component.

$checkboxTemplate

public string $checkboxTemplate = "<div class=\"checkbox\">\n{beginLabel}\n{input}\n{labelTitle}\n{endLabel}\n{error}\n{hint}\n</div>"

the template for checkboxes in default layout

$enableAjaxValidation

public bool|null $enableAjaxValidation

whether to enable AJAX-based data validation. If not set, it will take the value of [[ActiveForm::enableAjaxValidation]].

$enableClientValidation

public bool|null $enableClientValidation

whether to enable client-side data validation. If not set, it will take the value of [[ActiveForm::enableClientValidation]].

$enableError

public bool $enableError = true

whether to render the error. Default is true except for layout inline.

$enableLabel

public bool $enableLabel = true

whether to render the label. Default is true.

$errorOptions

public array<string|int, mixed> $errorOptions = ['class' => 'help-block']

the default options for the error tags. The parameter passed to [[error()]] will be merged with this property when rendering the error tag. The following special options are recognized:

  • tag: the tag name of the container element. Defaults to div. Setting it to false will not render a container tag. See also [[\yii\helpers\Html::tag()]].
  • encode: whether to encode the error output. Defaults to true.

If you set a custom id for the error element, you may need to adjust the [[$selectors]] accordingly.

Tags
see
Html::renderTagAttributes()

for details on how attributes are being rendered.

$hintOptions

public array<string|int, mixed> $hintOptions = ['class' => 'hint-block']

the default options for the hint tags. The parameter passed to [[hint()]] will be merged with this property when rendering the hint tag. The following special options are recognized:

  • tag: the tag name of the container element. Defaults to div. Setting it to false will not render a container tag. See also [[\yii\helpers\Html::tag()]].
Tags
see
Html::renderTagAttributes()

for details on how attributes are being rendered.

$horizontalCheckboxTemplate

public string $horizontalCheckboxTemplate = "{beginWrapper}\n<div class=\"checkbox\">\n{beginLabel}\n{input}\n{labelTitle}\n{endLabel}\n</div>\n{error}\n{endWrapper}\n{hint}"

the template for checkboxes in horizontal layout

$horizontalCssClasses

public null|array<string|int, mixed> $horizontalCssClasses = []

CSS grid classes for horizontal layout. This must be an array with these keys:

  • 'offset' the offset grid class to append to the wrapper if no label is rendered
  • 'label' the label grid class
  • 'wrapper' the wrapper grid class
  • 'error' the error grid class
  • 'hint' the hint grid class

$horizontalRadioTemplate

public string $horizontalRadioTemplate = "{beginWrapper}\n<div class=\"radio\">\n{beginLabel}\n{input}\n{labelTitle}\n{endLabel}\n</div>\n{error}\n{endWrapper}\n{hint}"

the template for radio buttons in horizontal layout

$inline

public bool $inline = false

whether to render [[checkboxList()]] and [[radioList()]] inline.

$inlineCheckboxListTemplate

public string $inlineCheckboxListTemplate = "{label}\n{beginWrapper}\n{input}\n{error}\n{endWrapper}\n{hint}"

the template for inline checkboxLists

$inlineRadioListTemplate

public string $inlineRadioListTemplate = "{label}\n{beginWrapper}\n{input}\n{error}\n{endWrapper}\n{hint}"

the template for inline radioLists

$inputOptions

public array<string|int, mixed> $inputOptions = ['class' => 'form-control']

the default options for the input tags. The parameter passed to individual input methods (e.g. [[textInput()]]) will be merged with this property when rendering the input tag.

If you set a custom id for the input element, you may need to adjust the [[$selectors]] accordingly.

Tags
see
Html::renderTagAttributes()

for details on how attributes are being rendered.

$inputTemplate

public string|null $inputTemplate

optional template to render the {input} placeholder content

$labelOptions

public array<string|int, mixed> $labelOptions = ['class' => 'control-label']

the default options for the label tags. The parameter passed to [[label()]] will be merged with this property when rendering the label tag.

Tags
see
Html::renderTagAttributes()

for details on how attributes are being rendered.

$model

public Model $model

the data model that this field is associated with.

$options

public array<string|int, mixed> $options = ['class' => 'form-group']

the HTML attributes (name-value pairs) for the field container tag. The values will be HTML-encoded using [[Html::encode()]]. If a value is null, the corresponding attribute will not be rendered. The following special options are recognized:

  • tag: the tag name of the container element. Defaults to div. Setting it to false will not render a container tag. See also [[\yii\helpers\Html::tag()]].

If you set a custom id for the container element, you may need to adjust the [[$selectors]] accordingly.

Tags
see
Html::renderTagAttributes()

for details on how attributes are being rendered.

$parts

public array<string|int, mixed> $parts = []

different parts of the field (e.g. input, label). This will be used together with [[template]] to generate the final field HTML code. The keys are the token names in [[template]], while the values are the corresponding HTML code. Valid tokens include {input}, {label} and {error}. Note that you normally don't need to access this property directly as it is maintained by various methods of this class.

$radioTemplate

public string $radioTemplate = "<div class=\"radio\">\n{beginLabel}\n{input}\n{labelTitle}\n{endLabel}\n{error}\n{hint}\n</div>"

the template for radios in default layout

$selectors

public array<string|int, mixed> $selectors = []

the jQuery selectors for selecting the container, input and error tags. The array keys should be container, input, and/or error, and the array values are the corresponding selectors. For example, ['input' => '#my-input'].

The container selector is used under the context of the form, while the input and the error selectors are used under the context of the container.

You normally do not need to set this property as the default selectors should work well for most cases.

$template

public string $template = "{label}\n{input}\n{hint}\n{error}"

the template that is used to arrange the label, the input field, the error message and the hint text. The following tokens will be replaced when [[render()]] is called: {label}, {input}, {error} and {hint}.

$validateOnBlur

public bool|null $validateOnBlur

whether to perform validation when the input field loses focus. If not set, it will take the value of [[ActiveForm::validateOnBlur]].

$validateOnChange

public bool|null $validateOnChange

whether to perform validation when the value of the input field is changed. If not set, it will take the value of [[ActiveForm::validateOnChange]].

$validateOnType

public bool|null $validateOnType

whether to perform validation while the user is typing in the input field. If not set, it will take the value of [[ActiveForm::validateOnType]].

Tags
see
validationDelay

$validationDelay

public int|null $validationDelay

number of milliseconds that the validation should be delayed when the user types in the field and [[validateOnType]] is set true. If not set, it will take the value of [[ActiveForm::validationDelay]].

$wrapperOptions

public array<string|int, mixed> $wrapperOptions = []

options for the wrapper tag, used in the {beginWrapper} placeholder

$_behaviors

private array<string|int, Behavior>|null $_behaviors

the attached behaviors (behavior name => behavior). This is null when not initialized.

$_events

private array<string|int, mixed> $_events = []

the attached event handlers (event name => handlers)

$_eventWildcards

private array<string|int, mixed> $_eventWildcards = []

the event handlers attached for wildcard patterns (event name wildcard => handlers)

Tags
since
2.0.14

$_inputId

private string $_inputId

this property holds a custom input id if it was set using [[inputOptions]] or in one of the $options parameters of the input* methods.

$_skipLabelFor

private bool $_skipLabelFor = false

if "for" field label attribute should be skipped.

Methods

__call()

Calls the named method which is not a class method.

public __call(string $name, array<string|int, mixed> $params) : mixed

This method will check if any attached behavior has the named method and will execute it if available.

Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.

Parameters
$name : string

the method name

$params : array<string|int, mixed>

method parameters

Tags
throws
UnknownMethodException

when calling unknown method

Return values
mixed

the method return value

__clone()

This method is called after the object is created by cloning an existing one.

public __clone() : mixed

It removes all behaviors because they are attached to the old object.

__construct()

Constructor.

public __construct([mixed $config = [] ]) : mixed
Parameters
$config : mixed = []

name-value pairs that will be used to initialize the object properties

__get()

Returns the value of a component property.

public __get(string $name) : mixed

This method will check in the following order and act accordingly:

  • a property defined by a getter: return the getter result
  • a property of a behavior: return the behavior property value

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $value = $component->property;.

Parameters
$name : string

the property name

Tags
throws
UnknownPropertyException

if the property is not defined

throws
InvalidCallException

if the property is write-only.

see
__set()
Return values
mixed

the property value or the value of a behavior's property

__isset()

Checks if a property is set, i.e. defined and not null.

public __isset(string $name) : bool

This method will check in the following order and act accordingly:

  • a property defined by a setter: return whether the property is set
  • a property of a behavior: return whether the property is set
  • return false for non existing properties

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing isset($component->property).

Parameters
$name : string

the property name or the event name

Tags
see
https://www.php.net/manual/en/function.isset.php
Return values
bool

whether the named property is set

__set()

Sets the value of a component property.

public __set(string $name, mixed $value) : mixed

This method will check in the following order and act accordingly:

  • a property defined by a setter: set the property value
  • an event in the format of "on xyz": attach the handler to the event "xyz"
  • a behavior in the format of "as xyz": attach the behavior named as "xyz"
  • a property of a behavior: set the behavior property value

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $component->property = $value;.

Parameters
$name : string

the property name or the event name

$value : mixed

the property value

Tags
throws
UnknownPropertyException

if the property is not defined

throws
InvalidCallException

if the property is read-only.

see
__get()

__toString()

PHP magic method that returns the string representation of this object.

public __toString() : string
Return values
string

the string representation of this object.

__unset()

Sets a component property to be null.

public __unset(string $name) : mixed

This method will check in the following order and act accordingly:

  • a property defined by a setter: set the property value to be null
  • a property of a behavior: set the property value to be null

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing unset($component->property).

Parameters
$name : string

the property name

Tags
throws
InvalidCallException

if the property is read only.

see
https://www.php.net/manual/en/function.unset.php

attachBehavior()

Attaches a behavior to this component.

public attachBehavior(string $name, string|array<string|int, mixed>|Behavior $behavior) : Behavior

This method will create the behavior object based on the given configuration. After that, the behavior object will be attached to this component by calling the [[Behavior::attach()]] method.

Parameters
$name : string

the name of the behavior.

$behavior : string|array<string|int, mixed>|Behavior

the behavior configuration. This can be one of the following:

  • a [[Behavior]] object
  • a string specifying the behavior class
  • an object configuration array that will be passed to [[Yii::createObject()]] to create the behavior object.
Tags
see
detachBehavior()
Return values
Behavior

the behavior object

attachBehaviors()

Attaches a list of behaviors to the component.

public attachBehaviors(array<string|int, mixed> $behaviors) : mixed

Each behavior is indexed by its name and should be a [[Behavior]] object, a string specifying the behavior class, or an configuration array for creating the behavior.

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

list of behaviors to be attached to the component

Tags
see
attachBehavior()

begin()

Renders the opening tag of the field container.

public begin() : string
Return values
string

the rendering result.

behaviors()

Returns a list of behaviors that this component should behave as.

public behaviors() : array<string|int, mixed>

Child classes may override this method to specify the behaviors they want to behave as.

The return value of this method should be an array of behavior objects or configurations indexed by behavior names. A behavior configuration can be either a string specifying the behavior class or an array of the following structure:

'behaviorName' => [
    'class' => 'BehaviorClass',
    'property1' => 'value1',
    'property2' => 'value2',
]

Note that a behavior class must extend from [[Behavior]]. Behaviors can be attached using a name or anonymously. When a name is used as the array key, using this name, the behavior can later be retrieved using [[getBehavior()]] or be detached using [[detachBehavior()]]. Anonymous behaviors can not be retrieved or detached.

Behaviors declared in this method will be attached to the component automatically (on demand).

Return values
array<string|int, mixed>

the behavior configurations.

canGetProperty()

Returns a value indicating whether a property can be read.

public canGetProperty(string $name[, bool $checkVars = true ][, bool $checkBehaviors = true ]) : bool

A property can be read if:

  • the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);
  • an attached behavior has a readable property of the given name (when $checkBehaviors is true).
Parameters
$name : string

the property name

$checkVars : bool = true

whether to treat member variables as properties

$checkBehaviors : bool = true

whether to treat behaviors' properties as properties of this component

Tags
see
canSetProperty()
Return values
bool

whether the property can be read

canSetProperty()

Returns a value indicating whether a property can be set.

public canSetProperty(string $name[, bool $checkVars = true ][, bool $checkBehaviors = true ]) : bool

A property can be written if:

  • the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);
  • an attached behavior has a writable property of the given name (when $checkBehaviors is true).
Parameters
$name : string

the property name

$checkVars : bool = true

whether to treat member variables as properties

$checkBehaviors : bool = true

whether to treat behaviors' properties as properties of this component

Tags
see
canGetProperty()
Return values
bool

whether the property can be written

checkbox()

Renders a checkbox.

public checkbox([mixed $options = [] ][, mixed $enclosedByLabel = true ]) : $this
Parameters
$options : 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 radio button. If not set, it will take the default value 0. This method will render a hidden input so that if the radio button is not checked and is submitted, the value of this attribute will still be submitted to the server via the hidden input. If you do not want any hidden input, you should explicitly set this option as null.
  • 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 coming from end users, you should [[Html::encode()|encode]] it to prevent XSS attacks. When this option is specified, the checkbox will be enclosed by a label tag. If you do not want any label, you should explicitly set this option as null.
  • labelOptions: array, the HTML attributes for the label tag. This is only used when the label option is specified.

The rest of the options will be rendered as the attributes of the resulting tag. The values will be HTML-encoded using [[Html::encode()]]. If a value is null, the corresponding attribute will not be rendered.

If you set a custom id for the input element, you may need to adjust the [[$selectors]] accordingly.

$enclosedByLabel : mixed = true

whether to enclose the checkbox within the label. If true, the method will still use [[template]] to layout the checkbox and the error message except that the checkbox is enclosed by the label tag.

Return values
$this

the field object itself.

checkboxList()

Renders a list of checkboxes.

public checkboxList(mixed $items[, mixed $options = [] ]) : $this
Parameters
$items : mixed

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

$options : mixed = []

options (name => config) for the checkbox list. For the list of available options please refer to the $options parameter of [[\yii\helpers\Html::activeCheckboxList()]].

Return values
$this

the field object itself.

className()

Returns the fully qualified name of this class.

public static className() : string
Tags
deprecated

since 2.0.14. On PHP >=5.5, use ::class instead.

Return values
string

the fully qualified name of this class.

detachBehavior()

Detaches a behavior from the component.

public detachBehavior(string $name) : Behavior|null

The behavior's [[Behavior::detach()]] method will be invoked.

Parameters
$name : string

the behavior's name.

Return values
Behavior|null

the detached behavior. Null if the behavior does not exist.

detachBehaviors()

Detaches all behaviors from the component.

public detachBehaviors() : mixed

dropDownList()

Renders a drop-down list.

public dropDownList(array<string|int, mixed> $items[, array<string|int, mixed> $options = [] ]) : $this

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

Parameters
$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 [[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.

For the list of available options please refer to the $options parameter of [[\yii\helpers\Html::activeDropDownList()]].

If you set a custom id for the input element, you may need to adjust the [[$selectors]] accordingly.

Return values
$this

the field object itself.

end()

Renders the closing tag of the field container.

public end() : string
Return values
string

the rendering result.

ensureBehaviors()

Makes sure that the behaviors declared in [[behaviors()]] are attached to this component.

public ensureBehaviors() : mixed

error()

Generates a tag that contains the first validation error of [[attribute]].

public error([array<string|int, mixed>|false $options = [] ]) : $this

Note that even if there is no validation error, this method will still return an empty error tag.

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

the tag options in terms of name-value pairs. It will be merged with [[errorOptions]]. The options will be rendered as the attributes of the resulting tag. The values will be HTML-encoded using [[Html::encode()]]. If this parameter is false, no error tag will be rendered.

The following options are specially handled:

  • tag: this specifies the tag name. If not set, div will be used. See also [[\yii\helpers\Html::tag()]].

If you set a custom id for the error element, you may need to adjust the [[$selectors]] accordingly.

Tags
see
errorOptions
Return values
$this

the field object itself.

fileInput()

Renders a file input.

public fileInput([array<string|int, mixed> $options = [] ]) : $this

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

Parameters
$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 [[Html::encode()]].

If you set a custom id for the input element, you may need to adjust the [[$selectors]] accordingly.

Return values
$this

the field object itself.

getBehavior()

Returns the named behavior object.

public getBehavior(string $name) : Behavior|null
Parameters
$name : string

the behavior name

Return values
Behavior|null

the behavior object, or null if the behavior does not exist

getBehaviors()

Returns all behaviors attached to this component.

public getBehaviors() : array<string|int, Behavior>
Return values
array<string|int, Behavior>

list of behaviors attached to this component

hasEventHandlers()

Returns a value indicating whether there is any handler attached to the named event.

public hasEventHandlers(string $name) : bool
Parameters
$name : string

the event name

Return values
bool

whether there is any handler attached to the event.

hasMethod()

Returns a value indicating whether a method is defined.

public hasMethod(string $name[, bool $checkBehaviors = true ]) : bool

A method is defined if:

  • the class has a method with the specified name
  • an attached behavior has a method with the given name (when $checkBehaviors is true).
Parameters
$name : string

the property name

$checkBehaviors : bool = true

whether to treat behaviors' methods as methods of this component

Return values
bool

whether the method is defined

hasProperty()

Returns a value indicating whether a property is defined for this component.

public hasProperty(string $name[, bool $checkVars = true ][, bool $checkBehaviors = true ]) : bool

A property is defined if:

  • the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);
  • an attached behavior has a property of the given name (when $checkBehaviors is true).
Parameters
$name : string

the property name

$checkVars : bool = true

whether to treat member variables as properties

$checkBehaviors : bool = true

whether to treat behaviors' properties as properties of this component

Tags
see
canGetProperty()
see
canSetProperty()
Return values
bool

whether the property is defined

hiddenInput()

Renders a hidden input.

public hiddenInput([array<string|int, mixed> $options = [] ]) : $this

Note that this method is provided for completeness. In most cases because you do not need to validate a hidden input, you should not need to use this method. Instead, you should use [[\yii\helpers\Html::activeHiddenInput()]].

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

Parameters
$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 [[Html::encode()]].

If you set a custom id for the input element, you may need to adjust the [[$selectors]] accordingly.

Return values
$this

the field object itself.

hint()

Renders the hint tag.

public hint(string|bool|null $content[, array<string|int, mixed> $options = [] ]) : $this
Parameters
$content : string|bool|null

the hint content. If null, the hint will be generated via [[Model::getAttributeHint()]]. If false, the generated field will not contain the hint part. Note that this will NOT be [[Html::encode()|encoded]].

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

the tag options in terms of name-value pairs. These will be rendered as the attributes of the hint tag. The values will be HTML-encoded using [[Html::encode()]].

The following options are specially handled:

  • tag: this specifies the tag name. If not set, div will be used. See also [[\yii\helpers\Html::tag()]].
Return values
$this

the field object itself.

init()

Initializes the object.

public init() : mixed

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

inline()

public inline([bool $value = true ]) : $this
Parameters
$value : bool = true

whether to render a inline list

Return values
$this

the field object itself Make sure you call this method before [[checkboxList()]] or [[radioList()]] to have any effect.

input()

Renders an input tag.

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

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

$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 [[Html::encode()]].

If you set a custom id for the input element, you may need to adjust the [[$selectors]] accordingly.

Return values
$this

the field object itself.

label()

Generates a label tag for [[attribute]].

public label([mixed $label = null ][, mixed $options = [] ]) : $this
Parameters
$label : mixed = null

the label to use. If null, the label will be generated via [[Model::getAttributeLabel()]]. If false, the generated field will not contain the label part. Note that this will NOT be [[Html::encode()|encoded]].

$options : mixed = []

the tag options in terms of name-value pairs. It will be merged with [[labelOptions]]. The options will be rendered as the attributes of the resulting tag. The values will be HTML-encoded using [[Html::encode()]]. If a value is null, the corresponding attribute will not be rendered.

Return values
$this

the field object itself.

listBox()

Renders a list box.

public listBox(array<string|int, mixed> $items[, array<string|int, mixed> $options = [] ]) : $this

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

Parameters
$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.

For the list of available options please refer to the $options parameter of [[\yii\helpers\Html::activeListBox()]].

If you set a custom id for the input element, you may need to adjust the [[$selectors]] accordingly.

Return values
$this

the field object itself.

off()

Detaches an existing event handler from this component.

public off(string $name[, callable|null $handler = null ]) : bool

This method is the opposite of [[on()]].

Note: in case wildcard pattern is passed for event name, only the handlers registered with this wildcard will be removed, while handlers registered with plain names matching this wildcard will remain.

Parameters
$name : string

event name

$handler : callable|null = null

the event handler to be removed. If it is null, all handlers attached to the named event will be removed.

Tags
see
on()
Return values
bool

if a handler is found and detached

on()

Attaches an event handler to an event.

public on(string $name, callable $handler[, mixed $data = null ][, bool $append = true ]) : mixed

The event handler must be a valid PHP callback. The following are some examples:

function ($event) { ... }         // anonymous function
[$object, 'handleClick']          // $object->handleClick()
['Page', 'handleClick']           // Page::handleClick()
'handleClick'                     // global function handleClick()

The event handler must be defined with the following signature,

function ($event)

where $event is an [[Event]] object which includes parameters associated with the event.

Since 2.0.14 you can specify event name as a wildcard pattern:

$component->on('event.group.*', function ($event) {
    Yii::trace($event->name . ' is triggered.');
});
Parameters
$name : string

the event name

$handler : callable

the event handler

$data : mixed = null

the data to be passed to the event handler when the event is triggered. When the event handler is invoked, this data can be accessed via [[Event::data]].

$append : bool = true

whether to append new event handler to the end of the existing handler list. If false, the new handler will be inserted at the beginning of the existing handler list.

Tags
see
off()

passwordInput()

Renders a password input.

public passwordInput([array<string|int, mixed> $options = [] ]) : $this

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

Parameters
$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 [[Html::encode()]].

If you set a custom id for the input element, you may need to adjust the [[$selectors]] accordingly.

Return values
$this

the field object itself.

radio()

Renders a radio button.

public radio([mixed $options = [] ][, mixed $enclosedByLabel = true ]) : $this
Parameters
$options : 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 radio button. If not set, it will take the default value 0. This method will render a hidden input so that if the radio button is not checked and is submitted, the value of this attribute will still be submitted to the server via the hidden input. If you do not want any hidden input, you should explicitly set this option as null.
  • label: string, a label displayed next to the radio button. 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 [[Html::encode()|encode]] it to prevent XSS attacks. When this option is specified, the radio button will be enclosed by a label tag. If you do not want any label, you should explicitly set this option as null.
  • labelOptions: array, the HTML attributes for the label tag. This is only used when the label option is specified.

The rest of the options will be rendered as the attributes of the resulting tag. The values will be HTML-encoded using [[Html::encode()]]. If a value is null, the corresponding attribute will not be rendered.

If you set a custom id for the input element, you may need to adjust the [[$selectors]] accordingly.

$enclosedByLabel : mixed = true

whether to enclose the radio within the label. If true, the method will still use [[template]] to layout the radio button and the error message except that the radio is enclosed by the label tag.

Return values
$this

the field object itself.

radioList()

Renders a list of radio buttons.

public radioList(mixed $items[, mixed $options = [] ]) : $this
Parameters
$items : mixed

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

$options : mixed = []

options (name => config) for the radio button list. For the list of available options please refer to the $options parameter of [[\yii\helpers\Html::activeRadioList()]].

Return values
$this

the field object itself.

render()

Renders the whole field.

public render([mixed $content = null ]) : string
Parameters
$content : mixed = null

the content within the field container. If null (not set), the default methods will be called to generate the label, error tag and input tag, and use them as the content. If a callable, it will be called to generate the content. The signature of the callable should be:

function ($field) {
    return $html;
}
Return values
string

the rendering result.

staticControl()

Renders Bootstrap static form control.

public staticControl([array<string|int, mixed> $options = [] ]) : $this
Parameters
$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
since
2.0.5
see
http://getbootstrap.com/css/#forms-controls-static
Return values
$this

the field object itself

textarea()

Renders a text area.

public textarea([array<string|int, mixed> $options = [] ]) : $this

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

Parameters
$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 [[Html::encode()]].

If you set a custom id for the textarea element, you may need to adjust the [[$selectors]] accordingly.

Return values
$this

the field object itself.

textInput()

Renders a text input.

public textInput([array<string|int, mixed> $options = [] ]) : $this

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

Parameters
$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 [[Html::encode()]].

The following special options are recognized:

  • maxlength: int|bool, when maxlength is set true and the model attribute is validated by a string validator, the maxlength option will take the value of [[\yii\validators\StringValidator::max]]. This is available since version 2.0.3.

Note that if you set a custom id for the input element, you may need to adjust the value of [[selectors]] accordingly.

Return values
$this

the field object itself.

trigger()

Triggers an event.

public trigger(string $name[, Event|null $event = null ]) : mixed

This method represents the happening of an event. It invokes all attached handlers for the event including class-level handlers.

Parameters
$name : string

the event name

$event : Event|null = null

the event instance. If not set, a default [[Event]] object will be created.

widget()

Renders a widget as the input of the field.

public widget(string $class[, array<string|int, mixed> $config = [] ]) : $this

Note that the widget must have both model and attribute properties. They will be initialized with [[model]] and [[attribute]] of this field, respectively.

If you want to use a widget that does not have model and attribute properties, please use [[render()]] instead.

While widgets extending from [[Widget]] work with active field, it is preferred to use [[InputWidget]] as a base class.

For example to use the [[MaskedInput]] widget to get some date input, you can use the following code, assuming that $form is your [[ActiveForm]] instance:

$form->field($model, 'date')->widget(\yii\widgets\MaskedInput::class, [
    'mask' => '99/99/9999',
]);

If you set a custom id for the input element, you may need to adjust the [[$selectors]] accordingly.

Parameters
$class : string

the widget class name.

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

name-value pairs that will be used to initialize the widget.

Tags
throws
Exception
Return values
$this

the field object itself.

addAriaAttributes()

Adds aria attributes to the input options.

protected addAriaAttributes(mixed &$options) : mixed
Parameters
$options : mixed

array input options

Tags
since
2.0.11

addErrorClassIfNeeded()

Adds validation class to the input options if needed.

protected addErrorClassIfNeeded(mixed &$options) : mixed
Parameters
$options : mixed

array input options

Tags
since
2.0.14

addRoleAttributes()

Add role attributes to the input options

protected addRoleAttributes(mixed &$options, string $role) : mixed
Parameters
$options : mixed

array input options

$role : string
Tags
since
2.0.16

adjustLabelFor()

Adjusts the `for` attribute for the label based on the input options.

protected adjustLabelFor(array<string|int, mixed> $options) : mixed
Parameters
$options : array<string|int, mixed>

the input options.

createLayoutConfig()

protected createLayoutConfig(array<string|int, mixed> $instanceConfig) : array<string|int, mixed>
Parameters
$instanceConfig : array<string|int, mixed>

the configuration passed to this instance's constructor

Return values
array<string|int, mixed>

the layout specific default configuration for this instance

getClientOptions()

Returns the JS options for the field.

protected getClientOptions() : array<string|int, mixed>
Return values
array<string|int, mixed>

the JS options.

getInputId()

Returns the HTML `id` of the input element of this form field.

protected getInputId() : string
Tags
since
2.0.7
Return values
string

the input id.

isAjaxValidationEnabled()

Checks if ajax validation enabled for the field.

protected isAjaxValidationEnabled() : bool
Tags
since
2.0.11
Return values
bool

isClientValidationEnabled()

Checks if client validation enabled for the field.

protected isClientValidationEnabled() : bool
Tags
since
2.0.11
Return values
bool

renderLabelParts()

protected renderLabelParts([string|null $label = null ][, array<string|int, mixed> $options = [] ]) : mixed
Parameters
$label : string|null = null

the label or null to use model label

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

the tag options

attachBehaviorInternal()

Attaches a behavior to this component.

private attachBehaviorInternal(string|int $name, string|array<string|int, mixed>|Behavior $behavior) : Behavior
Parameters
$name : string|int

the name of the behavior. If this is an integer, it means the behavior is an anonymous one. Otherwise, the behavior is a named one and any existing behavior with the same name will be detached first.

$behavior : string|array<string|int, mixed>|Behavior

the behavior to be attached

Return values
Behavior

the attached behavior.


        
On this page

Search results