codemix\bs3\ActiveForm

A Bootstrap 3 enhanced ActiveForm for Yii 2

Introduction

codemix\bs3\ActiveForm is an enhanced version of yii\widgets\ActiveForm. It mainly sets the appropriate ActiveField configuration for different Bootstrap form layouts, and adds some useful features to ActiveField. This gives you a wide range of rendering options for your Bootstrap 3 forms. Make sure you check out the example pages linked on top.

Example 1: Rendering features
<?php
use codemix\bs3\ActiveForm;
?>
<?php $form = ActiveForm::begin(['layout' => 'horizontal']) ?>
    <?= $form->field($model, 'demo', [
        'inputOptions' => [
            'placeholder' => $model->getAttributeLabel('demo'),
        ],
    ])->label(false); ?>
    <?= $form->field($model, 'demo')->dropDownList($items); ?>
    <?= $form->field($model, 'demo')->checkbox(); ?>
    <?= $form->field($model, 'demo')->inline()->radioList($items); ?>
<?php ActiveForm::end(); ?>

Note: This class does not include any Bootstrap 3 CSS or Javascript files. We recommend using the yii2-bootstrap extension to include those files.

Field Options

We have extended yii\widgets\ActiveField to add some useful features for Bootstrap 3 forms.

New Template Placeholders

There are a couple of new placeholders that you can use in the template option. Some of them are automatically configured, when you choose a form layout.

Placeholder Description
{beginLabel} The opening label tag (if you need more control over the label than with {label}).
{labelTitle} The label title, for use with {beginLabel} / {endLabel}.
{endLabel} The closing label tag.
{beginWrapper} The opening wrapper tag. This is only used for some elements and layouts.
{endWrapper} The closing wrapper tag. This is only used for some elements and layouts.

Options

Option Description
inline This option can be used with radioButtonList and checkBoxList elements to render inline list items. If set to true, it will automatically set the appropriate template and inputOptions (i.e. labelOptions). You can also call the inline() method instead.
label This can be set to false to disable the field label. Default is true.
inputTemplate An optional template for the input element. If set, this will be used to render the content of the {input} placeholder. This can be used to create input groups (see the examples). The default is none.
wrapperOptions The options of the wrapper tag.
horizontalCssClasses This is only used with horizontal layout. It defines the column grid classes to use for the label, the wrapper, the error and the hint. There's also an offset class that will be applied if there's no label (e.g. on checkboxes or if label was disabled). The default ist:
[
    'offset' => 'col-sm-offset-3',
    'label' => 'col-sm-3',
    'wrapper' => 'col-sm-6',
    'error' => '',
    'hint' => 'col-sm-3',
]
                
enableErrorBlock Whether to render the error block. This is mainly useful for inline forms. By default this is true for standard and horizontal layout and false for inline layout.

Methods

Method Description
inline($value = true) Set the inline option of the field. This only affects radioButtonLists and checkBoxLists.
label($label = null, $options = []) This overrides yii\widgets\ActiveField::label() and disables the label if false is passed as $label value.

Form Options

You can preconfigure all the above field options in the fieldConfig property of the form. In addition there's now a layout property to configure the main Bootstrap 3 form layout.

Option Description
layout The form layout to use. Options are standard (default), horizontal and inline. Depending on the layout different default field settings will be used.