2017 © Pedro Peláez
 

yii2-extension yii2-form-creator-behavior

Yii2 behavior to create view form

image

evgeniydev/yii2-form-creator-behavior

Yii2 behavior to create view form

  • Friday, December 1, 2017
  • by evgeniy.bublik
  • Repository
  • 1 Watchers
  • 0 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 14 % Grown

The README.md

Yii2 form create behavior

This is behavior which generate view form for methods CREATE and UPDATE., (*1)

Installation

The preferred way to install this extension is through composer., (*2)

Either run, (*3)

php composer.phar require  evgeniydev/yii2-form-creator-behavior

or add, (*4)

"evgeniydev/yii2-form-creator-behavior": "*"

to the require section of your composer.json file., (*5)

Basic usage

Model file (app/models/SomeModel.php), (*6)

...
use evgeniydev\yii2\behaviors\FormCreatorBehavior
...
class SomeModel extends \yii\base\Model
{
public $field1;
public $field2;
public $field3;
...
public function behaviors()
{
    return [
        ...
        'formBehavior' => [
            'class' => FormCreatorBehavior::className(),
            'attributes' => [
                'field1' => [ // <input type="text">
                    'type' => FormCreatorBehavior::TEXT_INPUT_TYPE,
                    'inputOptions' => [
                        'class' => 'someClass',
                        // other html options
                    ],
                ],
                'field2', // by default generate <input type="text">
                'field3' => [ // generate <select>...</select>
                    'type' => FormCreatorBehavior::DROPDOWNLIST_TYPE,
                    'items' => ['option1', 'option2'],
                    'inputOptions' => [
                        'prompt' => 'Choose option',
                    ],
                ],
                'field4' => function($form, $model) { // callable function to return view field for this form
                    return $form->field($model)
                }
                // other fields
            ],
        ]
        ...
    ];
}

Controller file (app/controllers/SomeController.php), (*7)

use app\models\SomeModel.php
...
public function actionCreate()
{
    $model = new SomeModel();
    if ($model->load(\Yii::$app->request->post()) && $model->save()) {
        //  model save
    }

    return $this->render('form', [
        'model' => $model,
    ]);
}

public function actionUpdate($id)
{
    $model = SomeModel::findOne($id);

    if (!$model) {
        // throw not found
    }
    if ($model->load(\Yii::$app->request->post()) && $model->save()) {
        //  model update
    }

    return $this->render('form', [
        'model' => $model,
    ]);
}
...

View file (app/views/some/form.php), (*8)

= $model->form; ?>

Other field types

Checkbox, (*9)

'field1' => [
    'type' => FormCreatorBehavior::CHECKBOX_TYPE,
    'inputOptions' => [
        'class' => 'someClass',
        // other html options
    ],
],

Checkbox list, (*10)

'field1' => [
    'type' => FormCreatorBehavior::CHECKBOXLIST_TYPE,
    'items' => ['check1', 'check2'],
    'inputOptions' => [
        'class' => 'someClass',
        // other html options
    ],
],

Widget, (*11)

'field1' => [
    'type' => FormCreatorBehavior::WIDGET_TYPE,
    'widgetClass' => '' // widget class name
    'widgetOptions' => [...], // widget options
],

List box, (*12)

'field1' => [
    'type' => FormCreatorBehavior::LISTBOX_TYPE,
    'items' => ['check1', 'check2'],
    'inputOptions' => [
        'class' => 'someClass',
        // other html options
    ],
],

Input with custom type, (*13)

'field1' => [ // <input type="tel">
    'type' => FormCreatorBehavior::INPUT_TYPE,
    'inputType' => 'tel',
    'inputOptions' => [
        'class' => 'someClass',
        // other html options
    ],
],

Full list constants types

  • TEXT_INPUT_TYPE - text input
  • TEXTAREA_TYPE - textarea
  • CHECKBOX_TYPE - checkbox
  • DROPDOWNLIST_TYPE - dropdownlist
  • WIDGET_TYPE - widget
  • INPUT_TYPE - input with custom type
  • HIDDEN_INPUT_TYPE - hidden input
  • FILE_INPUT_TYPE - file input
  • PASSWORD_INPUT_TYPE - password input
  • RADIO_TYPE - radio input
  • RADIOLIST_TYPE - radio list
  • CHECKBOXLIST_TYPE - checkboxe list
  • LISTBOX_TYPE - list box

Other configuration

Input with callable function function template: function($form, $model){ return ...; }, (*14)

'field1' => function($form, $model) { // callable function
    return $form->field($model, 'field1');
},

Label, (*15)

'field1' => [
    'type' => FormCreatorBehavior::TEXT_INPUT_TYPE,
    'label' => 'Some label', // or false, if want not show label
    ...
],

Hint (short form), (*16)

'field1' => [
    'type' => FormCreatorBehavior::TEXT_INPUT_TYPE,
    'hint' => 'Some hint',
    ...
],

or full form, (*17)

'field1' => [
    'type' => FormCreatorBehavior::TEXT_INPUT_TYPE,
    'hint' => [
        'content' => 'Some content', // hint content
        'options' => [...], // hint options
    ],
    ...
],

Form configuration, (*18)

'class' => FormCreatorBehavior::className(),
'attributes' => [...], // array attributes
'formOptions' => [
    'options' => [
        'enctype' => 'multipart/form-data'
    ],
    'id' => 'Some id',
    ... // other \yii\widgets\ActiveForm widget options
]

Template view, (*19)

'class' => FormCreatorBehavior::className(),
'attributes' => [...], // array attributes
'template' => '{items}{beginBlockButtons}{submitButton}{cancelButton}{endBlockButtons}', // template view

``` {items} - elements of form {beginBlockButtons} - open tag for block with form buttons {submitButton} - submit form button {cancelButton} - cancel form button {endBlockButtons} - end tag for block with buttons, (*20)

**Submit button options**
```php
'class' => FormCreatorBehavior::className(),
'attributes' => [...], // array attributes
'submitButtonOptions' => [
    'createButtonOptions' => [ // create button options
        'title' => 'Create', // text create button
        'tag' => 'input', // tag create button (input|button)
        'htmlOptions' => [...], // create button html options
    ],
    'updateButtonOptions' => [
        'title' => 'Update', // text update button
        'tag' => 'input', // tag update button (input|button)
        'htmlOptions' => [...], // update button html options
    ],
],

Cancel button options, (*21)

'class' => FormCreatorBehavior::className(),
'attributes' => [...], // array attributes
'cancelButtonOptions' => [
    'show' => true, // true or false, show cancel button
    'title' => 'Cancel', // text cancel button
    'action' => ['index'], // url to go cancel operation, by default is action index
    'htmlOptions' => [...], // cancel button html options
],

Wrapper block buttons form options, (*22)

'class' => FormCreatorBehavior::className(),
'attributes' => [...], // array attributes
'wrapperBlockButtonsOptions' => [
    'tag' => 'div', // tag name wrapper buttons block or false
    'htmlOptions' => [...], // cancel button html options
],

Tabs, (*23)

'class' => FormCreatorBehavior::className(),
'tabOptions' => [ // tab options
    'widgetName' => '...', // tab widget name, default is \yii\bootstrap\Tabs
    'widgetOptions' => [
        'keyNameContentField' => '...', // key name tab content field, default 'content'
        // ... other some widget options
    ],
    'tabs' => [
        [ // tab 1
            'tabAttributes' => ['someField1', 'someField2', ...], // list attributes on this tab,
            'content' => '{items}', // template of content tab, {items} to be replaced by fields attribues on this tab, 'content' if
                       // keyNameContentField = 'content', if keyNameContentField not equal 'content', then key 'content' key must be other
            // .. other tab options for this widget tab, example \yii\bootstrap\Tabs: 'title' => 'Some title',
        ],
        [ // tab 2
            'tabAttributes' => ['someField3', 'someField4', ...], // list attributes on this tab,
            // .. other tab options for this widget tab, example \yii\bootstrap\Tabs: 'title' => 'Some title',
        ],
        // ... other tabs
    ],
],

The Versions

01/12 2017

dev-master

9999999-dev https://github.com/jeka-deadline/yii2-FormCreatorBehavior

Yii2 behavior to create view form

  Sources   Download

BSD-3-Clause

The Requires

 

yii2 behavior

01/12 2017

1.0

1.0.0.0 https://github.com/jeka-deadline/yii2-FormCreatorBehavior

Yii2 behavior to create view form

  Sources   Download

BSD-3-Clause

The Requires

 

yii2 behavior