ActiveForm
API reference › Bootstrap 6 › ActiveForm
Renders a Bootstrap 6 form with default, horizontal, inline, or floating-label field layouts.
The layout selects the default ActiveField configuration; override it per form through $fieldConfig.
The inline layout adds row lg:row-cols-auto g-3 align-items-center to the form, and $nativeValidation
opts the form in to the Bootstrap :user-invalid styling with the data-bs-validate attribute.
See also: https://getbootstrap.com/docs/6.0/forms/layout/, https://getbootstrap.com/docs/6.0/forms/validation/.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$action | array| | The form action URL. Default: ''. | yii\ |
$ajaxDataType | string | The type of data that you’re expecting back from the server. Default: 'json'. | yii\ |
$ajaxParam | string | The name of the GET parameter indicating the validation request is an AJAX request. Default: 'ajax'. | yii\ |
$autoIdPrefix static | string | The prefix to the automatically generated widget IDs. Default: 'w'. | yii\ |
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$clientScript | array|yii\| | Client script class to use for client-side validation. Default: null. | yii\ |
$enableAjaxValidation | bool | Whether to enable AJAX-based data validation. Default: false. | yii\ |
$enableClientScript | bool | Whether to hook up yii.activeForm JavaScript plugin. Default: true. | yii\ |
$enableClientValidation | bool | Whether to enable client-side data validation. Default: true. | yii\ |
$encodeErrorSummary | bool | Whether to perform encoding on the error summary. Default: true. | yii\ |
$errorCssClass | string | CSS class added to the control when the attribute has validation errors. Default: 'is-invalid'. | ActiveForm |
$errorSummaryCssClass | string | CSS class of the error summary container. Default: 'alert theme-danger'. | ActiveForm |
$fieldClass | string | Field class used by field(); it must extend ActiveField. Default: \. | ActiveForm |
$fieldConfig | array|Closure | The default configuration used by field() when creating a new field object. Default: []. | yii\ |
$id | string | Id of the widget. | yii\ |
$layout | string | Form layout, one of the LAYOUT_* constants. Default: self::LAYOUT_DEFAULT. | ActiveForm |
$method | string | The form submission method. Default: 'post'. | yii\ |
$nativeValidation | bool| | Client-side validation styling: true renders data-bs-validate, 'valid' renders data-bs-validate="valid" (success styling too), false removes the attribute, and null keeps $options unchanged. Default: null. | ActiveForm |
$options | array | The HTML attributes (name-value pairs) for the form tag. Default: []. | yii\ |
$requiredCssClass | string | The CSS class that is added to a field container when the associated attribute is required. Default: 'required'. | yii\ |
$scrollToError | bool | Whether to scroll to the first error after validation. Default: true. Available since 2.0.6. | yii\ |
$scrollToErrorOffset | int | Offset in pixels that should be added when scrolling to the first error. Default: 0. Available since 2.0.11. | yii\ |
$successCssClass | string | CSS class added to the control when the attribute passes validation. Default: 'is-valid'. | ActiveForm |
$validateOnBlur | bool | Whether to perform validation when an input field loses focus. Default: true. | yii\ |
$validateOnChange | bool | Whether to perform validation when the value of an input field is changed. Default: true. | yii\ |
$validateOnSubmit | bool | Whether to perform validation when the form is submitted. Default: true. | yii\ |
$validateOnType | bool | Whether to perform validation while the user is typing in an input field. Default: false. | yii\ |
$validatingCssClass | string | The CSS class that is added to a field container when the associated attribute is being validated. Default: 'validating'. | yii\ |
$validationDelay | int | Number of milliseconds that the validation should be delayed when the user types in the field and $validateOnType is set true. Default: 500. | yii\ |
$validationStateOn | string | Element that receives the validation CSS classes; Bootstrap styles the control itself. Default: self::VALIDATION_STATE_ON_INPUT. | ActiveForm |
$validationUrl | array| | The URL for performing AJAX-based validation. Default: null. | yii\ |
$view | yii\ | The view object that can be used to render views or view files. | yii\ |
$viewPath read-only | string | The directory containing the view files for this widget. | yii\ |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
afterRun() | This method is invoked right after a widget is executed. | yii\ |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
beforeRun() | This method is invoked right before the widget is executed. | yii\ |
begin() static | Begins a widget. | yii\ |
beginField() | Begins a form field. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
end() static | Ends a widget. | yii\ |
endField() | Ends a form field. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
errorSummary() | Generates a summary of the validation errors. | yii\ |
field() | ActiveForm | |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getId() | Returns the ID of the widget. | yii\ |
getView() | Returns the view object that can be used to render views or view files. | yii\ |
getViewPath() | Returns the directory containing the view files for this widget. | yii\ |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | ActiveForm | |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
registerClientScript() | This registers the necessary JavaScript code. | yii\ |
render() | Renders a view. | yii\ |
renderFile() | Renders a view file. | yii\ |
run() | Runs the widget. | yii\ |
setId() | Sets the ID of the widget. | yii\ |
setView() | Sets the view object to be used by this widget. | yii\ |
trigger() | Triggers an event. | yii\ |
validate() static | Validates one or several models and returns an error message array indexed by the attribute IDs. | yii\ |
validateMultiple() static | Validates an array of model instances and returns an error message array indexed by the attribute IDs. | yii\ |
widget() static | Creates a widget instance and runs it. | yii\ |
getClientOptions() protected | yii\ |
Events
Section titled “Events”| Event | Type | Description | Defined by |
|---|---|---|---|
EVENT_INIT | yii\ | An event that is triggered when the widget is initialized via init(). Event name: 'init'. Available since 2.0.11. | yii\ |
EVENT_BEFORE_RUN | yii\ | An event raised right before executing a widget. Event name: 'beforeRun'. Available since 2.0.11. | yii\ |
EVENT_AFTER_RUN | yii\ | An event raised right after executing a widget. Event name: 'afterRun'. Available since 2.0.11. | yii\ |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
LAYOUT_DEFAULT | 'default' | Stacked labels, controls, hints, and feedback. | ActiveForm |
LAYOUT_FLOATING | 'floating' | Floating labels rendered inside a .form-floating wrapper. | ActiveForm |
LAYOUT_HORIZONTAL | 'horizontal' | Grid rows with the label and the control side by side. | ActiveForm |
LAYOUT_INLINE | 'inline' | Auto-sized columns with visually hidden labels and no error feedback. | ActiveForm |
LAYOUTS | [self::LAYOUT_DEFAULT, | Supported layouts. | ActiveForm |
VALIDATION_STATE_ON_CONTAINER | 'container' | Add validation state class to container tag Available since 2.0.14. | yii\ |
VALIDATION_STATE_ON_INPUT | 'input' | Add validation state class to input tag Available since 2.0.14. | yii\ |
Method details
Section titled “Method details”field()
Section titled “field() ”public function field(yii\base\Model $model, string $attribute, mixed[] $options = []): yii2\extensions\bootstrap6\ActiveFieldParameters
| Parameter | Type | Description |
|---|---|---|
$model | yii\ | Model holding the attribute. |
$attribute | string | Attribute name or expression. |
$options | mixed[] | Field configuration merged over $fieldConfig. |
Returns
Throws
yii\base\InvalidConfigException: if the configured field class does not extendActiveField.
init()
Section titled “init() ”public function init(): voidThrows
yii\base\InvalidConfigException: if$layoutor$nativeValidationis invalid.