ActiveField
extends Component
in package
ActiveField represents a form input field within an [[ActiveForm]].
For more details and usage information on ActiveField, see the guide article on forms.
Tags
Table of Contents
Properties
- $addAriaAttributes : bool
- $attribute : string
- $behaviors : array<string|int, Behavior>
- $enableAjaxValidation : bool|null
- $enableClientValidation : bool|null
- $errorOptions : array<string|int, mixed>
- $form : ActiveForm
- $hintOptions : array<string|int, mixed>
- $inputOptions : array<string|int, mixed>
- $labelOptions : array<string|int, mixed>
- $model : Model
- $options : array<string|int, mixed>
- $parts : array<string|int, mixed>
- $selectors : array<string|int, mixed>
- $template : string
- $validateOnBlur : bool|null
- $validateOnChange : bool|null
- $validateOnType : bool|null
- $validationDelay : int|null
- $_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.
- 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.
- 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.
- 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.
- 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
$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.
$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]].
$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 todiv
. Setting it tofalse
will not render a container tag. See also [[\yii\helpers\Html::tag()]]. -
encode
: whether to encode the error output. Defaults totrue
.
If you set a custom id
for the error element, you may need to adjust the [[$selectors]] accordingly.
Tags
$form
public
ActiveForm
$form
the form that this field is associated with.
$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 todiv
. Setting it tofalse
will not render a container tag. See also [[\yii\helpers\Html::tag()]].
Tags
$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
$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
$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 todiv
. Setting it tofalse
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
$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.
$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
$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]].
$_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
$_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
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([array<string|int, mixed> $config = [] ]) : mixed
The default implementation does two things:
- Initializes the object with the given configuration
$config
. - Call [[init()]].
If this method is overridden in a child class, it is recommended that
- the last parameter of the constructor is a configuration array, like
$config
here. - call the parent implementation at the end of the constructor.
Parameters
- $config : array<string|int, 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
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
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
__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
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
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
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
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
Return values
bool —whether the property can be written
checkbox()
Renders a checkbox.
public
checkbox([array<string|int, mixed> $options = [] ][, bool $enclosedByLabel = true ]) : $this
This method will generate the checked
tag attribute according to the model attribute value.
Parameters
- $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 radio button. If not set, it will take the default value0
. 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 asnull
. -
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 asnull
. -
labelOptions
: array, the HTML attributes for the label tag. This is only used when thelabel
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 : bool = 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(array<string|int, mixed> $items[, array<string|int, mixed> $options = [] ]) : $this
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
- $items : array<string|int, 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 : array<string|int, 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
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
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
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()]]. Iffalse
, 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.
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([string|null|false $label = null ][, array<string|int, mixed>|null $options = [] ]) : $this
Parameters
- $label : string|null|false = null
-
the label to use. If
null
, the label will be generated via [[Model::getAttributeLabel()]]. Iffalse
, the generated field will not contain the label part. Note that this will NOT be [[Html::encode()|encoded]]. - $options : array<string|int, mixed>|null = []
-
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
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
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([array<string|int, mixed> $options = [] ][, bool $enclosedByLabel = true ]) : $this
This method will generate the checked
tag attribute according to the model attribute value.
Parameters
- $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 radio button. If not set, it will take the default value0
. 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 asnull
. -
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 asnull
. -
labelOptions
: array, the HTML attributes for the label tag. This is only used when thelabel
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 : bool = 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(array<string|int, mixed> $items[, array<string|int, mixed> $options = [] ]) : $this
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
- $items : array<string|int, 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 : array<string|int, 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([string|callable|null $content = null ]) : string
This method will generate the label, error tag, input tag and hint tag (if any), and assemble them into HTML according to [[template]].
Parameters
- $content : string|callable|null = 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.
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, whenmaxlength
is settrue
and the model attribute is validated by a string validator, themaxlength
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
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
addErrorClassIfNeeded()
Adds validation class to the input options if needed.
protected
addErrorClassIfNeeded(mixed &$options) : mixed
Parameters
- $options : mixed
-
array input options
Tags
addRoleAttributes()
Add role attributes to the input options
protected
addRoleAttributes(mixed &$options, string $role) : mixed
Parameters
- $options : mixed
-
array input options
- $role : string
Tags
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.
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
Return values
string —the input id.
isAjaxValidationEnabled()
Checks if ajax validation enabled for the field.
protected
isAjaxValidationEnabled() : bool
Tags
Return values
boolisClientValidationEnabled()
Checks if client validation enabled for the field.
protected
isClientValidationEnabled() : bool
Tags
Return values
boolattachBehaviorInternal()
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.