PHP Class yii\widgets\ActiveForm

..
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends yii\base\Widget
Show file Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$action array | string the form action URL. This parameter will be processed by [[\yii\helpers\Url::to()]].
$ajaxDataType the type of data that you're expecting back from the server.
$ajaxParam the name of the GET parameter indicating the validation request is an AJAX request.
$attributes the client validation options for individual attributes. Each element of the array represents the validation options for a particular attribute.
$enableAjaxValidation whether to enable AJAX-based data validation. If [[ActiveField::enableAjaxValidation]] is set, its value will take precedence for that input field.
$enableClientScript 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.
$enableClientValidation whether to enable client-side data validation. If [[ActiveField::enableClientValidation]] is set, its value will take precedence for that input field.
$encodeErrorSummary whether to perform encoding on the error summary.
$errorCssClass the CSS class that is added to a field container when the associated attribute has validation error.
$errorSummaryCssClass the default CSS class for the error summary container.
$fieldClass the default field class name when calling ActiveForm::field to create a new field.
$fieldConfig the default configuration used by ActiveForm::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: php function ($model, $attribute) The value of this property will be merged recursively with the $options parameter passed to ActiveForm::field.
$method 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: php $form = ActiveForm::begin([ 'method' => 'get', 'action' => ['controller/action'], ]);
$options the HTML attributes (name-value pairs) for the form tag.
$requiredCssClass the CSS class that is added to a field container when the associated attribute is required.
$scrollToError whether to scroll to the first error after validation.
$scrollToErrorOffset offset in pixels that should be added when scrolling to the first error.
$successCssClass the CSS class that is added to a field container when the associated attribute is successfully validated.
$validateOnBlur whether to perform validation when an input field loses focus. If [[ActiveField::$validateOnBlur]] is set, its value will take precedence for that input field.
$validateOnChange whether to perform validation when the value of an input field is changed. If [[ActiveField::validateOnChange]] is set, its value will take precedence for that input field.
$validateOnSubmit whether to perform validation when the form is submitted.
$validateOnType whether to perform validation while the user is typing in an input field. If [[ActiveField::validateOnType]] is set, its value will take precedence for that input field.
$validatingCssClass the CSS class that is added to a field container when the associated attribute is being validated.
$validationDelay number of milliseconds that the validation should be delayed when the user types in the field and [[validateOnType]] is set true. If [[ActiveField::validationDelay]] is set, its value will take precedence for that input field.
$validationUrl the URL for performing AJAX-based validation. This property will be processed by [[Url::to()]]. Please refer to [[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 Methods

Method Description
beginField ( Model $model, string $attribute, array $options = [] ) : string Begins a form field.
endField ( ) : string Ends a form field.
errorSummary ( Model | Model[] $models, array $options = [] ) : string Generates a summary of the validation errors.
field ( Model $model, string $attribute, array $options = [] ) : ActiveField Generates a form field.
init ( ) Initializes the widget.
run ( ) Runs the widget.
validate ( Model $model, mixed $attributes = null ) : array Validates one or several models and returns an error message array indexed by the attribute IDs.
validateMultiple ( array $models, mixed $attributes = null ) : array Validates an array of model instances and returns an error message array indexed by the attribute IDs.

Protected Methods

Method Description
getClientOptions ( ) : array Returns the options for the form JS widget.

Method Details

beginField() public method

This method will create a new form field and returns its opening tag. You should call ActiveForm::endField afterwards.
See also: endField()
See also: field()
public beginField ( Model $model, string $attribute, array $options = [] ) : string
$model yii\base\Model the data model.
$attribute string the attribute name or expression. See [[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

This method will return the closing tag of an active form field started by ActiveForm::beginField.
public endField ( ) : string
return string the closing tag of the form field.

errorSummary() public method

If there is no validation error, an empty error summary markup will still be generated, but it will be hidden.
See also: errorSummaryCssClass
public errorSummary ( Model | Model[] $models, array $options = [] ) : string
$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

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 field ( Model $model, string $attribute, array $options = [] ) : ActiveField
$model yii\base\Model the data model.
$attribute string the attribute name or expression. See [[Html::getAttributeName()]] for the format about attribute expression.
$options array the additional configurations for the field object. These are properties of [[ActiveField]] or a subclass, depending on the value of [[fieldClass]].
return ActiveField the created ActiveField object.

getClientOptions() protected method

Returns the options for the form JS widget.
protected getClientOptions ( ) : array
return array the options.

init() public method

This renders the form open tag.
public init ( )

run() public method

This registers the necessary JavaScript code and renders the form close tag.
public run ( )

validate() public static method

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: php $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: php ActiveForm::validate($model1, $model2, ...);
public static validate ( Model $model, mixed $attributes = null ) : array
$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

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: php ... 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 validateMultiple ( array $models, mixed $attributes = null ) : array
$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.

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
return array | string

$ajaxDataType public property

the type of data that you're expecting back from the server.
public $ajaxDataType

$ajaxParam public property

the name of the GET parameter indicating the validation request is an AJAX request.
public $ajaxParam

$attributes public property

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

$enableAjaxValidation public property

whether to enable AJAX-based data validation. If [[ActiveField::enableAjaxValidation]] is set, its value will take precedence for that input field.
public $enableAjaxValidation

$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.
public $enableClientScript

$enableClientValidation public property

whether to enable client-side data validation. If [[ActiveField::enableClientValidation]] is set, its value will take precedence for that input field.
public $enableClientValidation

$encodeErrorSummary public property

whether to perform encoding on the error summary.
public $encodeErrorSummary

$errorCssClass public property

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

$errorSummaryCssClass public property

the default CSS class for the error summary container.
See also: errorSummary()
public $errorSummaryCssClass

$fieldClass public property

the default field class name when calling ActiveForm::field to create a new field.
See also: fieldConfig
public $fieldClass

$fieldConfig public property

the default configuration used by ActiveForm::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: php function ($model, $attribute) The value of this property will be merged recursively with the $options parameter passed to ActiveForm::field.
See also: fieldClass
public $fieldConfig

$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: php $form = ActiveForm::begin([ 'method' => 'get', 'action' => ['controller/action'], ]);
public $method

$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 $options

$requiredCssClass public property

the CSS class that is added to a field container when the associated attribute is required.
public $requiredCssClass

$scrollToError public property

whether to scroll to the first error after validation.
Since: 2.0.6
public $scrollToError

$scrollToErrorOffset public property

offset in pixels that should be added when scrolling to the first error.
Since: 2.0.11
public $scrollToErrorOffset

$successCssClass public property

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

$validateOnBlur public property

whether to perform validation when an input field loses focus. If [[ActiveField::$validateOnBlur]] is set, its value will take precedence for that input field.
public $validateOnBlur

$validateOnChange public property

whether to perform validation when the value of an input field is changed. If [[ActiveField::validateOnChange]] is set, its value will take precedence for that input field.
public $validateOnChange

$validateOnSubmit public property

whether to perform validation when the form is submitted.
public $validateOnSubmit

$validateOnType public property

whether to perform validation while the user is typing in an input field. If [[ActiveField::validateOnType]] is set, its value will take precedence for that input field.
See also: validationDelay
public $validateOnType

$validatingCssClass public property

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

$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 [[ActiveField::validationDelay]] is set, its value will take precedence for that input field.
public $validationDelay

$validationUrl public property

the URL for performing AJAX-based validation. This property will be processed by [[Url::to()]]. Please refer to [[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 $validationUrl