kartik-v/yii2-builder Build forms (single-row or multi-row/tabular) easily for Yii Framework 2.0

extensionwidgetformbuilder

Krajee Logo
yii2-builder Donate       kartikv

[![Latest Stable Version](https://poser.pugx.org/kartik-v/yii2-builder/v/stable)](https://packagist.org/packages/kartik-v/yii2-builder) [![Latest Unstable Version](https://poser.pugx.org/kartik-v/yii2-builder/v/unstable)](https://packagist.org/packages/kartik-v/yii2-builder) [![License](https://poser.pugx.org/kartik-v/yii2-builder/license)](https://packagist.org/packages/kartik-v/yii2-builder) [![Total Downloads](https://poser.pugx.org/kartik-v/yii2-builder/downloads)](https://packagist.org/packages/kartik-v/yii2-builder) [![Monthly Downloads](https://poser.pugx.org/kartik-v/yii2-builder/d/monthly)](https://packagist.org/packages/kartik-v/yii2-builder) [![Daily Downloads](https://poser.pugx.org/kartik-v/yii2-builder/d/daily)](https://packagist.org/packages/kartik-v/yii2-builder)

A form builder extension that allows you to build both single view and multi-view/tabular forms for Yii Framework 2.0. The extension contains these widgets:

  • Form
  • FormGrid
  • TabularForm

NOTE: Check the composer.json for this extension's requirements and dependencies.

Latest Release

Refer the CHANGE LOG for details on changes to various releases.

Docs & Demo

You can see detailed docs & demos and the API code documentation on usage of the extension.

Form

\kartik\builder\Form

The Form Builder widget allows you to build a form through a configuration array. Key features available:

  • Configure your form fields from a model extending yii\base\model or yii\db\ActiveRecord.
  • Ability to support various Bootstrap form layouts. Uses the advanced kartik\widgets\ActiveForm.
  • Use Bootstrap column/builder layout styling by just supplying columns property.
  • Build complex layouts (for example single, double, or multi columns in the same layout) - by reusing the widget for building your attributes.
  • Tweak ActiveForm defaults to control field options, styles, templates, and layouts.
  • Configure your own hints to display below each active field attribute.
  • Various Bootstrap styling features are available by default. However, one can easily customize and theme it to one's liking using any CSS framework.
  • Supports and renders HTML input types (uses kartik\widgets\ActiveField) including input widgets and more:
    • INPUT_TEXT or textInput
    • INPUT_TEXTAREA or textarea
    • INPUT_PASSWORD or passwordInput
    • INPUT_DROPDOWN_LIST or dropdownList
    • INPUT_LIST_BOX or listBox
    • INPUT_CHECKBOX or checkbox
    • INPUT_RADIO or radio
    • INPUT_CHECKBOX_LIST or checkboxList
    • INPUT_CHECKBOX_BUTTON_GROUP or checkboxList
    • INPUT_RADIO_LIST or radioList
    • INPUT_MULTISELECT or multiselect
    • INPUT_FILE or fileInput
    • INPUT_HTML5 or input
    • INPUT_WIDGET or widget
    • INPUT_HIDDEN or hiddenInput
    • INPUT_STATIC or staticInput
    • INPUT_HIDDEN_STATIC or hiddenStaticInput
    • INPUT_RAW or raw (any free text or html markup)

FormGrid

\kartik\builder\FormGrid

Create bootstrap grid layouts in a snap. The Form Grid Builder widget offers an easy way to configure your form inputs as a bootstrap grid layout and a single array configuration. It basically uses multiple instances of the \kartik\builder\Form widget above to generate this grid. One needs to just setup the rows for the grid, where each row will be an array configuration as needed by the Form widget. However, most of the common settings like model, form, columns etc. can be defaulted at FormGrid widget level.

Tabular Form

kartik\builder\TabularForm

The tabular form allows you to update information from multiple models (typically used in master-detail forms). Key features

  • Supports all input types as mentioned in the Form builder widget
  • The widget works like a Yii GridView and uses an ActiveDataProvider to read the models information.
  • Supports features of the builderview like pagination and sorting.
  • Allows you to highlight and select table rows
  • Allows you to add and configure action buttons for each row.
  • Configure your own hints to display below each active field attribute.
  • Various Bootstrap styling features are available by default. However, one can easily customize and theme it to one's liking using any CSS framework.
  • Advanced table styling, columns, and layout configuration by using the features available in the kartik\builder\GridView and the kartik\widgets\ActiveForm widget.
  • One can easily read and manage the tabular input data using the loadMultiple and validateMultiple functions in yii\base\Model.

NOTE: The TabularForm widget depends on and uses the yii2-grid module. Hence, the gridview module needs to be setup in your Yii configuration file.

'modules' => [
   'gridview' =>  [
        'class' => '\kartik\grid\Module'
    ]
];

IMPORTANT: You must follow one of the two options to setup your DataProvider or your columns to ensure primary key for each record is properly identified.

  • Option 1 (preferred): Setup your dataProvider query to use indexBy method to index your records by primary key. For example:
    
    $query = Model::find()->indexBy('id'); // where `id` is your primary key

$dataProvider = new ActiveDataProvider([ 'query' => $query, ]);

- **Option 2 (alternate):** You can setup the primary key attribute as one of your columns with a form input type (and hide if needed) - so that the models are appropriately updated via <code>loadMultiple</code> method (even if you reorder or sort the columns). You must also set this attribute to be `safe` in your model validation rules. This is been depicted in the example below.
```php
'attributes'=>[
    'id'=>[ // primary key attribute
        'type'=>TabularForm::INPUT_HIDDEN, 
        'columnOptions'=>['hidden'=>true]
    ], 
 ]

Demo

You can see detailed documentation on usage of the extension.

Installation

The preferred way to install this extension is through composer.

Note: You must set the minimum-stability to dev in the composer.json file in your application root folder before installation of this extension.

Either run

$ php composer.phar require kartik-v/yii2-builder "@dev"

or add

"kartik-v/yii2-builder": "@dev"

to the require section of your composer.json file.

Usage

Form

use kartik\builder\Form;
$form = ActiveForm::begin();
echo Form::widget([
    'model' => $model,
    'form' => $form,
    'columns' => 2,
    'attributes' => [
        'username' => ['type'=>Form::INPUT_TEXT, 'options'=>['placeholder'=>'Enter username...']],
        'password' => ['type'=>Form::INPUT_PASSWORD, 'options'=>['placeholder'=>'Enter password...']],
        'rememberMe' => ['type'=>Form::INPUT_CHECKBOX],
    ]
]);
ActiveForm::end();

FormGrid

use kartik\builder\Form;
use kartik\builder\FormGrid;
$form = ActiveForm::begin();
echo FormGrid::widget([
    'model' => $model,
    'form' => $form,
    'autoGenerateColumns' => true,
    'rows' => [
        [
            'attributes' => [
                'username' => ['type'=>Form::INPUT_TEXT, 'options'=>['placeholder'=>'Enter username...']],
                'password' => ['type'=>Form::INPUT_PASSWORD, 'options'=>['placeholder'=>'Enter password...']],
                'rememberMe' => ['type'=>Form::INPUT_CHECKBOX],
            ],
        ],
        [
            'attributes' => [
                'first_name' => ['type'=>Form::INPUT_TEXT, 'options'=>['placeholder'=>'Enter first name...']],
                'last_name' => ['type'=>Form::INPUT_TEXT, 'options'=>['placeholder'=>'Enter last name...']],
            ]
        ]
    ]
]);
ActiveForm::end();

TabularForm

use kartik\builder\TabularForm;
$form = ActiveForm::begin();
echo TabularForm::widget([
    'bsVersion' => '4.x',
    'form' => $form,
    'dataProvider' => $dataProvider,
    'attributes' => [
        'name' => ['type' => TabularForm::INPUT_TEXT],
        'color' => [
            'type' => TabularForm::INPUT_WIDGET, 
            'widgetClass' => \kartik\widgets\ColorInput::classname()
        ],
        'author_id' => [
            'type' => TabularForm::INPUT_DROPDOWN_LIST, 
            'items'=>ArrayHelper::map(Author::find()->orderBy('name')->asArray()->all(), 'id', 'name')
        ],
        'buy_amount' => [
            'type' => TabularForm::INPUT_TEXT, 
            'options'=>['class'=>'form-control text-right'], 
            'columnOptions'=>['hAlign'=>GridView::ALIGN_RIGHT]
        ],
        'sell_amount' => [
            'type' => TabularForm::INPUT_STATIC, 
            'columnOptions'=>['hAlign'=>GridView::ALIGN_RIGHT]
        ],
    ],
    'gridSettings' => [
        'floatHeader' => true,
        'panel' => [
            'heading' => '<i class="fas fa-book"></i> Manage Books',
            'type' => GridView::TYPE_PRIMARY,
            'after'=> 
                Html::a(
                    '<i class="fas fa-plus"></i> Add New', 
                    $createUrl, 
                    ['class'=>'btn btn-success']
                ) . '&nbsp;' . 
                Html::a(
                    '<i class="fas fa-times"></i> Delete', 
                    $deleteUrl, 
                    ['class'=>'btn btn-danger']
                ) . '&nbsp;' .
                Html::submitButton(
                    '<i class="fas fa-save"></i> Save', 
                    ['class'=>'btn btn-primary']
                )
        ]
    ]     
]); 
ActiveForm::end(); 

License

yii2-builder is released under the BSD-3-Clause License. See the bundled LICENSE.md for details.

Changelog

Change Log: yii2-builder

Version 1.6.9

Date: 05-Jan-2022

  • Enhance & standardize php docs for new website https://docs.krajee.com.
  • (enh #147): Enhancements to Form grid sub attributes layout for Bootstrap v5.x.
  • (bug #148): Correct Bootstrap version check method.
  • (bug #140,#141): Correct base widget class use.

Version 1.6.8

Date: 01-Sep-2021

Version 1.6.7

Date: 18-Jun-2019

  • (enh #139): Allow page summary calculations for TabularForm.
  • (enh #138): Correct TabularForm rowSelectedClass parsing for Bootstrap 4.x.
  • (enh #118): Add visible setting within columnOptions settings for attributes.
  • Add sponsorship capability.

Version 1.6.6

Date: 26-Nov-2018

  • Update README.
  • (enh #137): Correct form validation for TabularForm.
  • Better version dependencies.

Version 1.6.5

Date: 26-Sep-2018

  • Enhancements in parsing Bootstrap CSS classes (ref: kartik-v/yii2-krajee-base#100).

Version 1.6.4

Date: 20-Sep-2018

  • Enhancements to support Bootstrap v4.x.
  • Move all source code to src directory.

Version 1.6.3

Date: 29-Aug-2017

  • (enh #127): Better empty content validation.
  • (enh #126): Enhance hidden input rendering to not display additional markup.
  • Update copyright year to current.
  • Chronological sorting of issues and enhancements in CHANGE.log.
  • Include github contribution and issue/PR log templates.

Version 1.6.2

Date: 19-Nov-2016

  • Enhance and complete PHP documentation for all classes in the extension.
  • (enh #112): Correct prepend and append for INPUT_STATIC.
  • (enh #108, #110): Add support for configuring attribute visibility.
  • (enh #95): Add functionality for Bootstrap Checkbox Button Group and Radio Button Group.

Version 1.6.1

Date: 17-Jun-2015

  • (enh #87): Set composer ## Version dependencies.
  • (enh #83): More correct Closure input parse for Form widget.
  • (enh #82): Add content before and after in Form builder.
  • (enh #81): Allow attribute settings properties to be setup as Closure in TabularForm::attributes.
  • (enh #77): Enhancements for horizontal form styles.
  • (enh #74): New gridClass property to allow using widget extending kartik\grid\GridView.
  • (enh #70): Better default actionColumn settings.
  • (enh #69): Enhance code methods and code format including PHP doc.
  • (enh #68): Enhancements to Form sub attributes layout and styles.
  • (enh #67): New ActiveFormEvent class.
  • (enh #66): New input type INPUT_HIDDEN_STATIC
  • (enh #61): Fix array format attribute naming for non ActiveDataProvider.
  • (enh #58): New staticValue property as part of attributes configuration
  • (enh #57): New property staticOnly for Form and TabularForm.
  • (bug #56): Fix INPUT_STATIC to work better with TabularForm.
  • Code formatting fixes and JS Lint changes.
  • (enh #55): Composite keys handling for tabular forms.
  • (enh #52): Ability to render the entire form in static mode.

Version 1.6.0

Date: 28-Jan-2015

  • (enh #50): Enable format ability for INPUT_STATIC types in TabularForm.
  • (enh #51): Add support for rendering hidden inputs.
  • (enh #49): Enhance docs for key attribute in TabularForm to ensure loadMultiple works right.
  • (enh #47): Default column size based on device size set in ActiveForm formConfig.
  • (enh #46): Add new labelSpan property as part of builder attributes configuration.
  • (enh #45): Add support for hidden input.
  • (enh #43): Enhance rendering of fields for horizontal, vertical, and inline forms.
  • (enh #42): Add client validation plugin for nested attribute block.
  • (enh #39): Allow nested child attributes to be rendered together in one column.
  • Code format updates as per Yii2 coding style.
  • (enh #38): Set dependencies to kartik\form\ActiveForm.

Version 1.5.0

Date: 03-Dec-2014

  • New FormTrait added for better code reuse.
  • (enh #36): New additional options for attribute settings to control markup and styles.
    • prepend: string, (optional) any markup to prepend before the input
    • append: string, (optional) any markup to append before the input
    • container: array, (optional) HTML attributes for the div container to wrap the field group (including input and label for all forms. This also includes error & hint blocks for active forms). If not set or empty, no container will be wrapped.
    • inputContainer: array, (optional) HTML attributes for the div container to wrap the input control only. If not set or empty, no container will be wrapped. Will be applied only when NOT using with active form.
    • labelOptions: array, (optional) the HTML attributes for the label. Will be applied only when NOT using with active form and only if label is set.
  • (enh #35): Accelerate form building with new global attribute defaults.
  • (enh #34): Enhance to support normal forms without model in addition to ActiveForm.
  • (enh #33): Support all data providers extending from yii\data\BaseDataProvider for TabularForm widget.

Version 1.4.0

Date: 01-Dec-2014

  • (enh #30): Enhance Form Builder for horizontal forms and ability to customize labels.

Version 1.3.0

Date: 27-Nov-2014

  • Update copyright year to 2014.
  • (enh #29): Set default rowHighlight to true and rowSelected to TYPE_DANGER.
  • (enh #28): Set gridSettings['toggleData'] to false to prevent form nesting.
  • (enh #20): Fix code style and PHP documentation.

Version 1.2.0

Date: 11-Nov-2014

  • (bug #26): Correct instance of ActiveForm validation for FormGrid.
  • (bug #25): Correct package dependencies.
  • Set release to stable.
  • Set dependency on Krajee base component.
  • (enh #24): Create new FormGrid widget. Create bootstrap grid layouts in a snap.
  • (enh #23): Create autoGenerateColumns property for Form Builder

Version 1.1.0

Date: 07-Jul-2014

  • PSR 4 alias change
  • allow empty items for dropdown lists
  • (enh #13): Added ability to configure and display hints below each active field attribute.
  • (enh #1 through #12): Various optimization fixes.

Version 1.0.0

Date: 01-May-2014

Initial release

Statistics

Downloads
GitHub Stars
GitHub Forks

Releases

Comments



v1.6.9 is the latest of 16 releases



BSD-3-Clause license
Stats
100 github stars & 50 github forks
487 downloads in the last day
12052 downloads in the last 30 days
872280 total downloads