Class yii\widgets\ActiveForm

Inheritanceyii\widgets\ActiveForm » yii\base\Widget » yii\base\Component » yii\base\BaseObject
Implementsyii\base\Configurable, yii\base\ViewContextInterface
Subclassesyii\bootstrap\ActiveForm
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/widgets/ActiveForm.php

ActiveForm is a widget that builds an interactive HTML form for one or multiple data models.

For more details and usage information on ActiveForm, see the guide article on forms.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$action array|string The form action URL. yii\widgets\ActiveForm
$ajaxDataType string The type of data that you're expecting back from the server. yii\widgets\ActiveForm
$ajaxParam string The name of the GET parameter indicating the validation request is an AJAX request. yii\widgets\ActiveForm
$attributes array The client validation options for individual attributes. yii\widgets\ActiveForm
$autoIdPrefix string The prefix to the automatically generated widget IDs. yii\base\Widget
$behaviors yii\base\Behavior[] List of behaviors attached to this component yii\base\Component
$counter integer A counter used to generate $id for widgets. yii\base\Widget
$enableAjaxValidation boolean Whether to enable AJAX-based data validation. yii\widgets\ActiveForm
$enableClientScript boolean Whether to hook up `yii. yii\widgets\ActiveForm
$enableClientValidation boolean Whether to enable client-side data validation. yii\widgets\ActiveForm
$encodeErrorSummary boolean Whether to perform encoding on the error summary. yii\widgets\ActiveForm
$errorCssClass string The CSS class that is added to a field container when the associated attribute has validation error. yii\widgets\ActiveForm
$errorSummaryCssClass string The default CSS class for the error summary container. yii\widgets\ActiveForm
$fieldClass string The default field class name when calling field() to create a new field. yii\widgets\ActiveForm
$fieldConfig array|Closure The default configuration used by field() when creating a new field object. yii\widgets\ActiveForm
$id string ID of the widget. yii\base\Widget
$method string The form submission method. yii\widgets\ActiveForm
$options array The HTML attributes (name-value pairs) for the form tag. yii\widgets\ActiveForm
$requiredCssClass string The CSS class that is added to a field container when the associated attribute is required. yii\widgets\ActiveForm
$scrollToError boolean Whether to scroll to the first error after validation. yii\widgets\ActiveForm
$scrollToErrorOffset integer Offset in pixels that should be added when scrolling to the first error. yii\widgets\ActiveForm
$stack yii\base\Widget[] The widgets that are currently being rendered (not ended). yii\base\Widget
$successCssClass string The CSS class that is added to a field container when the associated attribute is successfully validated. yii\widgets\ActiveForm
$validateOnBlur boolean Whether to perform validation when an input field loses focus. yii\widgets\ActiveForm
$validateOnChange boolean Whether to perform validation when the value of an input field is changed. yii\widgets\ActiveForm
$validateOnSubmit boolean Whether to perform validation when the form is submitted. yii\widgets\ActiveForm
$validateOnType boolean Whether to perform validation while the user is typing in an input field. yii\widgets\ActiveForm
$validatingCssClass string The CSS class that is added to a field container when the associated attribute is being validated. yii\widgets\ActiveForm
$validationDelay integer Number of milliseconds that the validation should be delayed when the user types in the field and $validateOnType is set true. yii\widgets\ActiveForm
$validationStateOn string Where to render validation state class Could be either "container" or "input". yii\widgets\ActiveForm
$validationUrl array|string The URL for performing AJAX-based validation. yii\widgets\ActiveForm
$view yii\web\View The view object that can be used to render views or view files. yii\base\Widget
$viewPath string The directory containing the view files for this widget. yii\base\Widget

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\Component
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of a component property. yii\base\Component
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Component
__set() Sets the value of a component property. yii\base\Component
__unset() Sets a component property to be null. yii\base\Component
afterRun() This method is invoked right after a widget is executed. yii\base\Widget
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
beforeRun() This method is invoked right before the widget is executed. yii\base\Widget
begin() Begins a widget. yii\base\Widget
beginField() Begins a form field. yii\widgets\ActiveForm
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Component
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Component
className() Returns the fully qualified name of this class. yii\base\BaseObject
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
end() Ends a widget. yii\base\Widget
endField() Ends a form field. yii\widgets\ActiveForm
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
errorSummary() Generates a summary of the validation errors. yii\widgets\ActiveForm
field() Generates a form field. yii\widgets\ActiveForm
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getId() Returns the ID of the widget. yii\base\Widget
getView() Returns the view object that can be used to render views or view files. yii\base\Widget
getViewPath() Returns the directory containing the view files for this widget. yii\base\Widget
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\Component
hasProperty() Returns a value indicating whether a property is defined for this component. yii\base\Component
init() Initializes the widget. yii\widgets\ActiveForm
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
registerClientScript() This registers the necessary JavaScript code. yii\widgets\ActiveForm
render() Renders a view. yii\base\Widget
renderFile() Renders a view file. yii\base\Widget
run() Runs the widget. yii\widgets\ActiveForm
setId() Sets the ID of the widget. yii\base\Widget
setView() Sets the view object to be used by this widget. yii\base\Widget
trigger() Triggers an event. yii\base\Component
validate() Validates one or several models and returns an error message array indexed by the attribute IDs. yii\widgets\ActiveForm
validateMultiple() Validates an array of model instances and returns an error message array indexed by the attribute IDs. yii\widgets\ActiveForm
widget() Creates a widget instance and runs it. yii\base\Widget

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
getClientOptions() Returns the options for the form JS widget. yii\widgets\ActiveForm

Events

Hide inherited events

EventTypeDescriptionDefined By
EVENT_AFTER_RUN yii\base\WidgetEvent An event raised right after executing a widget. (available since version 2.0.11) yii\base\Widget
EVENT_BEFORE_RUN yii\base\WidgetEvent An event raised right before executing a widget. (available since version 2.0.11) yii\base\Widget
EVENT_INIT yii\base\Event An event that is triggered when the widget is initialized via init(). (available since version 2.0.11) yii\base\Widget

Constants

Hide inherited constants

ConstantValueDescriptionDefined By
VALIDATION_STATE_ON_CONTAINER 'container' Add validation state class to container tag yii\widgets\ActiveForm
VALIDATION_STATE_ON_INPUT 'input' Add validation state class to input tag yii\widgets\ActiveForm

Property Details

$action public property

The form action URL. This parameter will be processed by yii\helpers\Url::to().

See also $method for specifying the HTTP method for this form.

public array|string $action ''
$ajaxDataType public property

The type of data that you're expecting back from the server.

public string $ajaxDataType 'json'
$ajaxParam public property

The name of the GET parameter indicating the validation request is an AJAX request.

public string $ajaxParam 'ajax'
$attributes public property

The client validation options for individual attributes. Each element of the array represents the validation options for a particular attribute.

public array $attributes = []
$enableAjaxValidation public property

Whether to enable AJAX-based data validation. If yii\widgets\ActiveField::$enableAjaxValidation is set, its value will take precedence for that input field.

$enableClientScript public property

Whether to hook up yii.activeForm JavaScript plugin. This property must be set true if you want to support client validation and/or AJAX validation, or if you want to take advantage of the yii.activeForm plugin. When this is false, the form will not generate any JavaScript.

See also registerClientScript().

$enableClientValidation public property

Whether to enable client-side data validation. If yii\widgets\ActiveField::$enableClientValidation is set, its value will take precedence for that input field.

$encodeErrorSummary public property

Whether to perform encoding on the error summary.

$errorCssClass public property

The CSS class that is added to a field container when the associated attribute has validation error.

public string $errorCssClass 'has-error'
$errorSummaryCssClass public property

The default CSS class for the error summary container.

See also errorSummary().

public string $errorSummaryCssClass 'error-summary'
$fieldClass public property

The default field class name when calling field() to create a new field.

See also $fieldConfig.

public string $fieldClass 'yii\widgets\ActiveField'
$fieldConfig public property

The default configuration used by field() when creating a new field object. This can be either a configuration array or an anonymous function returning a configuration array. If the latter, the signature should be as follows:

function ($model, $attribute)

The value of this property will be merged recursively with the $options parameter passed to field().

See also $fieldClass.

$method public property

The form submission method. This should be either post or get. Defaults to post.

When you set this to get you may see the url parameters repeated on each request. This is because the default value of $action is set to be the current request url and each submit will add new parameters instead of replacing existing ones. You may set $action explicitly to avoid this:

$form = ActiveForm::begin([
    'method' => 'get',
    'action' => ['controller/action'],
]);
public string $method 'post'
$options public property

The HTML attributes (name-value pairs) for the form tag.

See also yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.

public array $options = []
$requiredCssClass public property

The CSS class that is added to a field container when the associated attribute is required.

public string $requiredCssClass 'required'
$scrollToError public property (available since version 2.0.6)

Whether to scroll to the first error after validation.

public boolean $scrollToError true
$scrollToErrorOffset public property (available since version 2.0.11)

Offset in pixels that should be added when scrolling to the first error.

$successCssClass public property

The CSS class that is added to a field container when the associated attribute is successfully validated.

public string $successCssClass 'has-success'
$validateOnBlur public property

Whether to perform validation when an input field loses focus. If yii\widgets\ActiveField::$validateOnBlur is set, its value will take precedence for that input field.

public boolean $validateOnBlur true
$validateOnChange public property

Whether to perform validation when the value of an input field is changed. If yii\widgets\ActiveField::$validateOnChange is set, its value will take precedence for that input field.

public boolean $validateOnChange true
$validateOnSubmit public property

Whether to perform validation when the form is submitted.

public boolean $validateOnSubmit true
$validateOnType public property

Whether to perform validation while the user is typing in an input field. If yii\widgets\ActiveField::$validateOnType is set, its value will take precedence for that input field.

See also $validationDelay.

public boolean $validateOnType false
$validatingCssClass public property

The CSS class that is added to a field container when the associated attribute is being validated.

public string $validatingCssClass 'validating'
$validationDelay public property

Number of milliseconds that the validation should be delayed when the user types in the field and $validateOnType is set true. If yii\widgets\ActiveField::$validationDelay is set, its value will take precedence for that input field.

$validationStateOn public property (available since version 2.0.14)

Where to render validation state class Could be either "container" or "input". Default is "container".

public string $validationStateOn self::VALIDATION_STATE_ON_CONTAINER
$validationUrl public property

The URL for performing AJAX-based validation. This property will be processed by yii\helpers\Url::to(). Please refer to yii\helpers\Url::to() for more details on how to configure this property. If this property is not set, it will take the value of the form's action attribute.

public array|string $validationUrl null

Method Details

beginField() public method

Begins a form field.

This method will create a new form field and returns its opening tag. You should call endField() afterwards.

See also:

public string beginField ( $model, $attribute, $options = [] )
$model yii\base\Model

The data model.

$attribute string

The attribute name or expression. See yii\helpers\Html::getAttributeName() for the format about attribute expression.

$options array

The additional configurations for the field object.

return string

The opening tag.

endField() public method

Ends a form field.

This method will return the closing tag of an active form field started by beginField().

public string endField ( )
return string

The closing tag of the form field.

throws yii\base\InvalidCallException

if this method is called without a prior beginField() call.

errorSummary() public method

Generates a summary of the validation errors.

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

See also $errorSummaryCssClass.

public string errorSummary ( $models, $options = [] )
$models yii\base\Model|yii\base\Model[]

The model(s) associated with this form.

$options array

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.

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

return string

The generated error summary.

field() public method

Generates a form field.

A form field is associated with a model and an attribute. It contains a label, an input and an error message and use them to interact with end users to collect their inputs for the attribute.

See also $fieldConfig.

public yii\widgets\ActiveField field ( $model, $attribute, $options = [] )
$model yii\base\Model

The data model.

$attribute string

The attribute name or expression. See yii\helpers\Html::getAttributeName() for the format about attribute expression.

$options array

The additional configurations for the field object. These are properties of yii\widgets\ActiveField or a subclass, depending on the value of $fieldClass.

return yii\widgets\ActiveField

The created ActiveField object.

getClientOptions() protected method

Returns the options for the form JS widget.

protected array getClientOptions ( )
return array

The options.

init() public method

Initializes the widget.

This renders the form open tag.

public void init ( )
registerClientScript() public method (available since version 2.0.12)

This registers the necessary JavaScript code.

public void registerClientScript ( )
run() public method

Runs the widget.

This registers the necessary JavaScript code and renders the form open and close tags.

public void run ( )
throws yii\base\InvalidCallException

if beginField() and endField() calls are not matching.

validate() public static method

Validates one or several models and returns an error message array indexed by the attribute IDs.

This is a helper method that simplifies the way of writing AJAX validation code.

For example, you may use the following code in a controller action to respond to an AJAX validation request:

$model = new Post;
$model->load(Yii::$app->request->post());
if (Yii::$app->request->isAjax) {
    Yii::$app->response->format = Response::FORMAT_JSON;
    return ActiveForm::validate($model);
}
// ... respond to non-AJAX request ...

To validate multiple models, simply pass each model as a parameter to this method, like the following:

ActiveForm::validate($model1, $model2, ...);
public static array validate ( $model, $attributes null )
$model yii\base\Model

The model to be validated.

$attributes mixed

List of attributes that should be validated. If this parameter is empty, it means any attribute listed in the applicable validation rules should be validated.

When this method is used to validate multiple models, this parameter will be interpreted as a model.

return array

The error message array indexed by the attribute IDs.

validateMultiple() public static method

Validates an array of model instances and returns an error message array indexed by the attribute IDs.

This is a helper method that simplifies the way of writing AJAX validation code for tabular input.

For example, you may use the following code in a controller action to respond to an AJAX validation request:

// ... load $models ...
if (Yii::$app->request->isAjax) {
    Yii::$app->response->format = Response::FORMAT_JSON;
    return ActiveForm::validateMultiple($models);
}
// ... respond to non-AJAX request ...
public static array validateMultiple ( $models, $attributes null )
$models array

An array of models to be validated.

$attributes mixed

List of attributes that should be validated. If this parameter is empty, it means any attribute listed in the applicable validation rules should be validated.

return array

The error message array indexed by the attribute IDs.