Class kartik\form\ActiveForm

Inheritancekartik\form\ActiveForm » yii\widgets\ActiveForm » yii\base\Widget » yii\base\Component » yii\base\BaseObject
Implementskartik\base\BootstrapInterface, yii\base\Configurable, yii\base\ViewContextInterface
Uses Traitskartik\base\BootstrapTrait
Subclasseskartik\widgets\ActiveForm
Available since version1.0

ActiveForm is a widget that builds an interactive HTML form for one or multiple data models and extends the yii\widgets\ActiveForm widget to handle various bootstrap form types and new functionality.

For example,

// Horizontal Form
$form = ActiveForm::begin([
     'id' => 'form-signup',
     'type' => ActiveForm::TYPE_HORIZONTAL
]);
// Inline Form
$form = ActiveForm::begin([
     'id' => 'form-login',
     'type' => ActiveForm::TYPE_INLINE
     'fieldConfig' => ['autoPlaceholder'=>true]
]);
// Horizontal Form Configuration
$form = ActiveForm::begin([
     'id' => 'form-signup',
     'type' => ActiveForm::TYPE_HORIZONTAL
     'formConfig' => ['labelSpan' => 2, 'deviceSize' => ActiveForm::SIZE_SMALL]
]);

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
$bsColCssPrefixes array The bootstrap grid column css prefixes mapping, the key is the bootstrap versions, and the value is an array containing the sizes and their corresponding grid column css prefixes. kartik\base\BootstrapTrait
$bsCssMap array CSS conversion mappings across bootstrap library versions. kartik\base\BootstrapTrait
$bsExtBasename integer The yii2 bootstrap extension base name (readonly property available via getter method getBsExtBasename()) kartik\base\BootstrapTrait
$bsVer integer Bootstrap version number currently set (readonly property available via getter method getBsVer()) kartik\base\BootstrapTrait
$bsVersion integer|string The bootstrap library version that you wish to use for this specific extension / widget. kartik\base\BootstrapTrait
$counter integer A counter used to generate $id for widgets. yii\base\Widget
$defaultBtnCss string Default bootstrap button CSS (readonly property available via getter method getDefaultBtnCss()) kartik\base\BootstrapTrait
$defaultIconPrefix string Default icon prefix (readonly property available via getter method getDefaultIconPrefix()) kartik\base\BootstrapTrait
$disabled boolean|Closure Whether all inputs in form are to be disabled kartik\form\ActiveForm
$dropdownClass string Bootstrap dropdown class name based on currently configured bootstrap version (readonly property available via getter method getDropdownClass()) kartik\base\BootstrapTrait
$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. kartik\form\ActiveForm
$fieldConfig array|Closure The default configuration used by field() when creating a new field object. yii\widgets\ActiveForm
$formConfig array The configuration for the form. kartik\form\ActiveForm
$formLayoutStyle array The form layout style configuration. kartik\form\ActiveForm
$fullSpan integer The bootstrap grid width. kartik\form\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
$inlineFormCssClass array|string CSS classes that will be appended for inline form. kartik\form\ActiveForm
$method string The form submission method. yii\widgets\ActiveForm
$options array HTML attributes for the form tag. kartik\form\ActiveForm
$readonly boolean|Closure Whether all inputs in form are to be readonly. kartik\form\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
$srOnlyCss string kartik\form\ActiveForm
$stack yii\base\Widget[] The widgets that are currently being rendered (not ended). yii\base\Widget
$staticOnly boolean|Closure Whether all data in form are to be rendered as Bootstrap styled static text inputs kartik\form\ActiveForm
$successCssClass string The CSS class that is added to a field container when the associated attribute is successfully validated. yii\widgets\ActiveForm
$tooltipStyleFeedback boolean Whether to render tooltip styled error and success messages. kartik\form\ActiveForm
$type string Form orientation type (for bootstrap styling). kartik\form\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|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

Protected Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$_bsVer integer Current bootstrap version number kartik\base\BootstrapTrait
$_defaultBtnCss string Default bootstrap button CSS kartik\base\BootstrapTrait
$_defaultIconPrefix string Default icon CSS prefix kartik\base\BootstrapTrait
$_isBs4 boolean Flag to detect whether bootstrap 4. kartik\base\BootstrapTrait

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
addCssClass() Adds bootstrap CSS class to options by parsing the bootstrap version for the specified Bootstrap CSS type. kartik\base\BootstrapTrait
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() kartik\form\ActiveForm
getBSClass() Gets the respective Bootstrap class based on currently configured bootstrap version. kartik\base\BootstrapTrait
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getBsVer() Gets the current set bootstrap version number. kartik\base\BootstrapTrait
getCssClass() Gets bootstrap css class by parsing the bootstrap version for the specified BS CSS type. kartik\base\BootstrapTrait
getDefaultBtnCss() Gets the default button CSS kartik\base\BootstrapTrait
getDefaultIconPrefix() Gets the default icon css prefix kartik\base\BootstrapTrait
getDropdownClass() Gets the respective bootstrap dropdown class name based on currently configured bootstrap version. kartik\base\BootstrapTrait
getFormLayoutStyle() Gets form layout style configuration. This method is used by kartik\field\FieldRange widget. kartik\form\ActiveForm
getId() Returns the ID of the widget. yii\base\Widget
getSrOnlyCss() Gets Screen Reader Only CSS class kartik\form\ActiveForm
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. kartik\form\ActiveForm
isBs() Validate Bootstrap version kartik\base\BootstrapTrait
isBs4() Validate if Bootstrap 4.x version. kartik\base\BootstrapTrait
isHorizontal() Whether a horizontal layout form kartik\form\ActiveForm
isInline() Whether an inline layout form kartik\form\ActiveForm
isVertical() Whether a vertical layout form kartik\form\ActiveForm
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
registerAssets() Registers the assets for the kartik\form\ActiveForm widget. kartik\form\ActiveForm
registerClientScript() This registers the necessary JavaScript code. yii\widgets\ActiveForm
removeCssClass() Removes bootstrap CSS class from options by parsing the bootstrap version for the specified Bootstrap CSS type. kartik\base\BootstrapTrait
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
configureBsVersion() Configures the bootstrap version settings kartik\base\BootstrapTrait
getBsExtBasename() The yii2-bootstrap extension base name. kartik\base\BootstrapTrait
getClientOptions() Returns the options for the form JS widget. yii\widgets\ActiveForm
initBsVersion() Initializes bootstrap versions for the widgets and asset bundles. kartik\base\BootstrapTrait
initForm() Initializes the form configuration array and parameters for the form. kartik\form\ActiveForm
isSameVersion() Compares two versions and checks if they are of the same major BS version. kartik\base\BootstrapTrait
parseVer() Parses and returns the major BS version kartik\base\BootstrapTrait

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
DEFAULT_LABEL_SPAN 2 kartik\form\ActiveForm
FULL_SPAN 12 kartik\form\ActiveForm
SCREEN_READER 'sr-only' kartik\form\ActiveForm
TYPE_FLOATING 'floating' kartik\form\ActiveForm
TYPE_HORIZONTAL 'horizontal' kartik\form\ActiveForm
TYPE_INLINE 'inline' kartik\form\ActiveForm
TYPE_VERTICAL 'vertical' kartik\form\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

$disabled public property

Whether all inputs in form are to be disabled. For advanced configuration (e.g. dynamically setting different flags for different fields), this can be setup as a Closure callback. When setup as a Closure callback, you can receive the model instance and the active field object instance as parameters. For example:

 'disabled' => function ($model, $field) {
    return in_array($field->$attribute, $model->disabledAttributes());
},
public boolean $disabled false
$fieldClass public property

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

public string $fieldClass 'kartik\form\ActiveField'
$formConfig public property

The configuration for the form. Takes in the following properties

  • labelSpan: integer, the bootstrap grid column width (usually between 1 to 12)
  • deviceSize: string, one of the bootstrap sizes (refer the ActiveForm::SIZE constants)
  • showLabels: boolean|string, whether to show labels (true), hide labels (false), or display only for SCREEN_READER. This is mainly useful for inline forms.
  • showErrors: boolean, whether to show errors (true) or hide errors (false). This is mainly useful for inline forms.
  • showHints: boolean, whether to show hints (true) or hide errors (false). Defaults to true. The hint will be rendered only if a valid hint has been set through the hint() method.

Defaults to,

[
    'labelSpan' => 2,
    'deviceSize' => ActiveForm::SIZE_MEDIUM,
    'showLabels' => true,
    'showErrors' => true,
    'showHints' => true
],
public array $formConfig = []
$formLayoutStyle public read-only property

The form layout style configuration.

$fullSpan public property

The bootstrap grid width. Defaults to FULL_SPAN.

public integer $fullSpan self::FULL_SPAN
$inlineFormCssClass public property

CSS classes that will be appended for inline form. Defaults to ['row', 'row-cols-lg-auto', 'g-3', 'align-items-center'] for Bootstrap 5.x and empty string for others.

$options public property

HTML attributes for the form tag. Defaults to:

['role' => 'form']
public array $options = [
    'role' => 'form'
]
$readonly public property

Whether all inputs in form are to be readonly. For advanced configuration (e.g. dynamically setting different flags for different fields), this can be setup as a Closure callback. When setup as a Closure callback, you can receive the model instance and the active field object instance as parameters. For example:

 'readonly' => function ($model, $field) {
    return in_array($field->$attribute, $model->readonlyAttributes());
},
public boolean $readonly false
$srOnlyCss public read-only property
public string getSrOnlyCss ( )
$staticOnly public property

Whether all inputs in form are to be rendered as Bootstrap styled static text inputs. For advanced configuration (e.g. dynamically setting different flags for different fields), this can be setup as a Closure callback. When setup as a Closure callback, you can receive the model instance and the active field object instance as parameters. For example:

 'staticOnly' => function ($model, $field) {
    return in_array($field->$attribute, $model->staticOnlyAttributes());
},
public boolean $staticOnly false
$tooltipStyleFeedback public property

Whether to render tooltip styled error and success messages. Applicable only for $bsVersion >= 4.

$type public property

Form orientation type (for bootstrap styling). Either TYPE_VERTICAL, TYPE_HORIZONTAL, or TYPE_INLINE. Defaults to TYPE_VERTICAL.

public string $type null

Method Details

field() public method

public kartik\form\ActiveField field ( Model $model, string $attribute, array )
$model
$attribute
array
return kartik\form\ActiveField
getFormLayoutStyle() public method

Gets form layout style configuration. This method is used by kartik\field\FieldRange widget.

public array getFormLayoutStyle ( )
return array

The form layout style configuration.

getSrOnlyCss() public method

Gets Screen Reader Only CSS class

public string getSrOnlyCss ( )
throws Exception
init() public method

Initializes the widget.

This renders the form open tag.

public void init ( )
throws yii\base\InvalidConfigException
initForm() protected method

Initializes the form configuration array and parameters for the form.

protected void initForm ( )
throws yii\base\InvalidConfigException
isHorizontal() public method

Whether a horizontal layout form

public boolean isHorizontal ( )
isInline() public method

Whether an inline layout form

public boolean isInline ( )
isVertical() public method

Whether a vertical layout form

public boolean isVertical ( )
registerAssets() public method

Registers the assets for the kartik\form\ActiveForm widget.

public void registerAssets ( )
throws \yii\base\InvalidConfigException|\Exception