Class yii\bootstrap5\ActiveForm

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

A Bootstrap 5 enhanced version of yii\widgets\ActiveForm.

This class mainly adds the $layout property to choose a Bootstrap 5 form layout. So for example to render a horizontal form you would:

use yii\bootstrap5\ActiveForm;

$form = ActiveForm::begin(['layout' => 'horizontal'])

This will set default values for the yii\bootstrap5\ActiveField to render horizontal form fields. In particular the template is set to {label} {beginWrapper} {input} {error} {endWrapper} {hint} and the horizontalCssClasses are set to:

[
    'offset' => 'offset-sm-3',
    'label' => 'col-sm-3',
    'wrapper' => 'col-sm-6',
    'error' => '',
    'hint' => 'col-sm-3',
]

To get a different column layout in horizontal mode you can modify those options through $fieldConfig:

$form = ActiveForm::begin([
    'layout' => 'horizontal',
    'fieldConfig' => [
        'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
        'horizontalCssClasses' => [
            'label' => 'col-sm-4',
            'offset' => 'offset-sm-4',
            'wrapper' => 'col-sm-8',
            'error' => '',
            'hint' => '',
        ],
    ],
]);

See also:

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\bootstrap5\ActiveForm
$errorSummaryCssClass string The default CSS class for the error summary container. yii\bootstrap5\ActiveForm
$fieldClass string The default field class name when calling field() to create a new field. yii\bootstrap5\ActiveForm
$fieldConfig array|Closure The default configuration used by field() when creating a new field object. yii\widgets\ActiveForm
$id string|null ID of the widget. Note that the type of this property differs in getter and setter. See getId() and setId() for details. yii\base\Widget
$layout string The form layout. yii\bootstrap5\ActiveForm
$method string The form submission method. yii\widgets\ActiveForm
$options array HTML attributes for the form tag. yii\bootstrap5\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\bootstrap5\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\bootstrap5\ActiveForm
$validationUrl array|string|null 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. Note that the type of this property differs in getter and setter. See getView() and setView() for details. 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() {@inheritDoc} yii\bootstrap5\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\bootstrap5\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
LAYOUT_DEFAULT 'default' Default form layout yii\bootstrap5\ActiveForm
LAYOUT_FLOATING 'floating' Floating labels form layout yii\bootstrap5\ActiveForm
LAYOUT_HORIZONTAL 'horizontal' Horizontal form layout yii\bootstrap5\ActiveForm
LAYOUT_INLINE 'inline' Inline form layout yii\bootstrap5\ActiveForm
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

$errorCssClass public property

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

public string $errorCssClass 'is-invalid'
$errorSummaryCssClass public property

The default CSS class for the error summary container.

public string $errorSummaryCssClass 'alert alert-danger'
$fieldClass public property

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

See also $fieldConfig.

public string $fieldClass = \yii\bootstrap5\ActiveField::class
$layout public property

The form layout. Either LAYOUT_DEFAULT, LAYOUT_HORIZONTAL or LAYOUT_INLINE. By choosing a layout, an appropriate default field configuration is applied. This will render the form fields with slightly different markup for each layout. You can override these defaults through $fieldConfig.

See also yii\bootstrap5\ActiveField for details on Bootstrap 5 field configuration.

public string $layout self::LAYOUT_DEFAULT
$options public property

HTML attributes for the form tag. Default is [].

public array $options = []
$successCssClass public property

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

public string $successCssClass 'is-valid'
$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_INPUT

Method Details

field() public method

{@inheritDoc}

public yii\bootstrap5\ActiveField|yii\widgets\ActiveField field ( $model, $attribute, $options = [] )
$model
$attribute
$options
init() public method

Initializes the widget.

This renders the form open tag.

public void init ( )
throws yii\base\InvalidConfigException