没有命名空间的类 yii yii\base yii\behaviors yii\caching yii\captcha yii\console yii\console\controllers yii\console\widgets yii\data yii\db yii\db\conditions yii\db\cubrid yii\db\cubrid\conditions yii\db\mssql yii\db\mssql\conditions yii\db\mysql yii\db\oci yii\db\oci\conditions yii\db\pgsql yii\db\sqlite yii\db\sqlite\conditions yii\di yii\filters yii\filters\auth yii\grid yii\helpers yii\i18n yii\log yii\mail yii\mutex yii\rbac yii\rest yii\test yii\validators yii\web yii\widgets

Class yii\widgets\ActiveForm

继承yii\widgets\ActiveForm » yii\base\Widget » yii\base\Component » yii\base\BaseObject
实现yii\base\Configurable, yii\base\ViewContextInterface
可用版本自2.0
源码 https://github.com/yiichina/yii2/blob/api/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.

公共属性

隐藏继承的属性

属性类型描述被定义在
$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

公共方法

隐藏继承的方法

方法描述被定义在
__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

受保护的方法

隐藏继承的方法

方法描述被定义在
getClientOptions() Returns the options for the form JS widget. yii\widgets\ActiveForm

Events

隐藏继承的事件

事件类型描述被定义在
EVENT_AFTER_RUN yii\base\WidgetEvent An event raised right after executing a widget. (自版本 2.0.11 可用) yii\base\Widget
EVENT_BEFORE_RUN yii\base\WidgetEvent An event raised right before executing a widget. (自版本 2.0.11 可用) yii\base\Widget
EVENT_INIT yii\base\Event An event that is triggered when the widget is initialized via init(). (自版本 2.0.11 可用) yii\base\Widget

常量

隐藏继承的常量

常量描述被定义在
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

属性详情

$action 公共 属性

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

参见 $method for specifying the HTTP method for this form.

public array|string $action ''
$ajaxDataType 公共 属性

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

public string $ajaxDataType 'json'
$ajaxParam 公共 属性

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

public string $ajaxParam 'ajax'
$attributes 公共 属性

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

public array $attributes = []
$enableAjaxValidation 公共 属性

Whether to enable AJAX-based data validation. If yii\widgets\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.

参见 registerClientScript().

$enableClientValidation 公共 属性

Whether to enable client-side data validation. If yii\widgets\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.

public string $errorCssClass 'has-error'
$errorSummaryCssClass 公共 属性

The default CSS class for the error summary container.

参见 errorSummary().

public string $errorSummaryCssClass 'error-summary'
$fieldClass 公共 属性

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

参见 $fieldConfig.

public string $fieldClass 'yii\widgets\ActiveField'
$fieldConfig 公共 属性

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

参见 $fieldClass.

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

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

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

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

public array $options = []
$requiredCssClass 公共 属性

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

public string $requiredCssClass 'required'
$scrollToError 公共 属性 (自版本 2.0.6 可用)

Whether to scroll to the first error after validation.

public boolean $scrollToError true
$scrollToErrorOffset 公共 属性 (自版本 2.0.11 可用)

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.

public string $successCssClass 'has-success'
$validateOnBlur 公共 属性

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 公共 属性

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 公共 属性

Whether to perform validation when the form is submitted.

public boolean $validateOnSubmit true
$validateOnType 公共 属性

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.

参见 $validationDelay.

public boolean $validateOnType false
$validatingCssClass 公共 属性

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

public string $validatingCssClass 'validating'
$validationDelay 公共 属性

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 公共 属性 (自版本 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 公共 属性

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

方法详情

beginField() 公共 方法

Begins a form field.

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

参见:

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() 公共 方法

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() 公共 方法

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.

参见 $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() 公共 方法

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.

参见 $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() 受保护 方法

Returns the options for the form JS widget.

protected array getClientOptions()
return array

The options.

init() 公共 方法

Initializes the widget.

This renders the form open tag.

public void init()
registerClientScript() 公共 方法 (自版本 2.0.12 可用)

This registers the necessary JavaScript code.

public void registerClientScript()
run() 公共 方法

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() 公共 静态 方法

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() 公共 静态 方法

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.