Class yii\bootstrap4\ActiveForm

Inheritanceyii\bootstrap4\ActiveForm » yii\widgets\ActiveForm
Source Code https://github.com/yiisoft/yii2-bootstrap4/blob/master/src/ActiveForm.php

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

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

use yii\bootstrap4\ActiveForm;

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

This will set default values for the yii\bootstrap4\ActiveField to render horizontal form fields. In particular the ActiveField::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

Property Type Description Defined By
$errorCssClass string The CSS class that is added to a field container when the associated attribute has validation error. yii\bootstrap4\ActiveForm
$errorSummaryCssClass yii\bootstrap4\ActiveForm
$fieldClass string The default field class name when calling field() to create a new field. yii\bootstrap4\ActiveForm
$layout string The form layout. yii\bootstrap4\ActiveForm
$options array HTML attributes for the form tag. yii\bootstrap4\ActiveForm
$successCssClass yii\bootstrap4\ActiveForm
$validationStateOn yii\bootstrap4\ActiveForm

Constants

Hide inherited constants

Constant Value Description Defined By
LAYOUT_DEFAULT 'default' Default form layout yii\bootstrap4\ActiveForm
LAYOUT_HORIZONTAL 'horizontal' Horizontal form layout yii\bootstrap4\ActiveForm
LAYOUT_INLINE 'inline' Inline form layout yii\bootstrap4\ActiveForm

Property Details

Hide inherited properties

$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
public $errorSummaryCssClass 'alert alert-danger'
$fieldClass public property

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

See also \yii\bootstrap4\fieldConfig.

public string $fieldClass 'yii\bootstrap4\ActiveField'
$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\bootstrap4\ActiveField for details on Bootstrap 4 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
public $successCssClass 'is-valid'
$validationStateOn public property
public $validationStateOn self::VALIDATION_STATE_ON_INPUT

Method Details

Hide inherited methods

field() public method

public yii\bootstrap4\ActiveField field ( $model, $attribute, $options = [] )
$model
$attribute
$options

                public function field($model, $attribute, $options = [])
{
    return parent::field($model, $attribute, $options);
}

            
init() public method

public void init ( )
throws \yii\base\InvalidConfigException

                public function init()
{
    if (!in_array($this->layout, [self::LAYOUT_DEFAULT, self::LAYOUT_HORIZONTAL, self::LAYOUT_INLINE])) {
        throw new InvalidConfigException('Invalid layout type: ' . $this->layout);
    }
    if ($this->layout === self::LAYOUT_INLINE) {
        Html::addCssClass($this->options, ['widget' => 'form-inline']);
    }
    parent::init();
}