Builder Details Form Grid Builder   Tips

Latest Stable Version Latest Unstable Version Total Downloads Monthly Downloads Daily Downloads
Thankful to Krajee!
to get more out of us.

NOTE: This extension depends on the kartik-v/yii2-krajee-base extension which in turn depends on the yiisoft/yii2-bootstrap extension. Check the composer.json for this extension's requirements and dependencies that may be updated by composer.

The yii2-builder package provides a couple of great form builder utilities for Yii Framework 2.0. The extension allows you to build both single-row and multi-row/tabular forms for Yii Framework 2.0. It also offers good compatibility with the grid system, form layouts, and table styles of Bootstrap 3.x.

The Form Grid Builder widget accepts similar settings to a Form widget, with some easier configuration and options to avoid code repetition. You basically can setup the rows for each bootstrap grid row and the grid columns can be auto generated, based on attribute settings for each row. The following settings are important:
  1. model Model, or ActiveRecord, model instance extending from yii\base\Model or yii\db\ActiveRecord. This is mandatory.

  2. form ActiveForm, the ActiveForm instance extending from kartik\widgets\ActiveForm. You normally would call it as $form = \kartik\widgets\ActiveForm::begin();. This is mandatory.

  3. formName string, the form name to be provided if not using with model and ActiveForm. One of form and model OR formName is mandatory.

  4. attributeDefaults array, the default settings that will be applied for all attributes. The array will be configured similar to a single attribute setting value in the $attributes array. One will typically default markup and styling like type, container, prepend, append etc. For example:

    // settings for horizontal form used without active form
    'attributeDefaults' => [
        'type' => Form::INPUT_TEXT,
        'labelOptions' => ['class'=>'col-md-2'],
        'inputContainer' => ['class'=>'col-md-10'],

    Note: The settings at the $attributes level will override these default settings.

  5. rows array, the most important part of your form grid builder configuration. This is mandatory. Each row (item) in this array, will be an array itself, that will consist of the widget properties required for the Form widget. Typically you can just pass the attributes for each row, and others can be defaulted from the FormGrid settings.

  6. columns integer, the number of columns in which to split the fields horizontally. If not set, defaults to 1 column. This property can be overridden at the rows configuration level for each row.

  7. autoGenerateColumns boolean, whether to auto calculate and generate the columns based on the count of attributes for each row. If not set, Defaults to true. If the count of attributes is > FormGrid::GRID_WIDTH (12) then no further columns will be generated. This property can be overridden at the rows configuration level for each row.

  8. columnSize string, the bootstrap device size for rendering each Bootstrap grid column. Applicable Defaults to `Form::SIZE_SMALL`. Can take in one of these options:

    • Form::SIZE_LARGE or lg

    • Form::SIZE_MEDIUM or md

    • Form::SIZE_SMALL or sm

    • Form::SIZE_TINY or xs

  9. columnOptions array, the HTML attributes for the grid columns. Applicable only if columns is greater than 1. The following special options are recognized:

    • colspan integer, the customized grid column span to use if you need assymmetric field columns in your layout. For the default bootstrap grid this is a number between 1 to 12.

  10. rowOptions integer, the HTML attributes for the grid rows. Applicable only if columns is greater than 1.

  11. options integer, the HTML attributes for the field/attributes container. The following options are additionally recognized:

    • tag: string, the HTML tag for the container. Defaults to fieldset.

Rendering a complex grid layout with FormGrid easily. In this you can have single, double or multi columns within the same form. You may also be planning to use various input types for your design and place as you want on your layouts. In order to achieve these, you can call the Form::widget multiple times with different settings as described below.


This example contains usage of various input types, including the INPUT_RAW example at the end. Note you can use INPUT_RAW type to define your own custom attributes. It also shows an example of how you can add hints to your active field attributes.
Scenario 3 Output
Account Info
Profile Info
Enter birthday (mm/dd/yyyy)
Type and select state
Choose your color
use kartik\widgets\ActiveForm;
use kartik\builder\Form;
use kartik\builder\FormGrid;
$form = ActiveForm::begin(['type'=>ActiveForm::TYPE_VERTICAL]);
echo FormGrid::widget([
            'contentBefore'=>'<legend class="text-info"><small>Account Info</small></legend>',
            'attributes'=>[       // 2 column layout
                'username'=>['type'=>Form::INPUT_TEXT, 'options'=>['placeholder'=>'Enter username...']],
                'password'=>['type'=>Form::INPUT_PASSWORD, 'options'=>['placeholder'=>'Enter password...']],
            'attributes'=>[       // 1 column layout
                'notes'=>['type'=>Form::INPUT_TEXTAREA, 'options'=>['placeholder'=>'Enter notes...']],
            'contentBefore'=>'<legend class="text-info"><small>Profile Info</small></legend>',
            'autoGenerateColumns'=>false, // override columns setting
            'attributes'=>[       // colspan example with nested attributes
                'address_detail' => [ 
                            'options'=>['placeholder'=>'Enter address...'],
                'birthday'=>['type'=>Form::INPUT_WIDGET, 'widgetClass'=>'\kartik\widgets\DatePicker', 'hint'=>'Enter birthday (mm/dd/yyyy)'],
                'state_1'=>['type'=>Form::INPUT_DROPDOWN_LIST, 'items'=>$model->typeahead_data, 'hint'=>'Type and select state'],
                'color'=>['type'=>Form::INPUT_WIDGET, 'widgetClass'=>'\kartik\widgets\ColorInput', 'hint'=>'Choose your color'],
            'attributes'=>[       // 3 column layout
                'rememberMe'=>[   // radio list
                    'items'=>[true=>'Yes', false=>'No'], 
                'brightness'=>[   // uses widget class with widget options
                    'label'=>Html::label('Brightness (%)'), 
                'actions'=>[    // embed raw HTML content
                    'value'=>  '<div style="text-align: right; margin-top: 20px">' . 
                        Html::resetButton('Reset', ['class'=>'btn btn-default']) . ' ' .
                        Html::submitButton('Submit', ['class'=>'btn btn-primary']) .