Small and easy form builder. You can store form configuration in form model., (*1)
, (*2)
, (*3)
Installation
The preferred way to install this extension is through composer., (*4)
Either run, (*5)
php composer.phar require --prefer-dist metalguardian/yii2-form-builder "~1.0"
or add, (*6)
"metalguardian/yii2-form-builder": "~1.0"
to the require section of your composer.json
file., (*7)
Usage
First of all you need write form config (you can store it in model class), (*8)
<?php
namespace app\models;
use metalguardian\formBuilder\ActiveFormBuilder;
/**
*/
class Example extends \yii\db\ActiveRecord
{
.....
/**
* @return array
*/
public function getFormConfig()
{
return [
'label' => [
'type' => ActiveFormBuilder::INPUT_TEXT,
],
'content' => [
'type' => ActiveFormBuilder::INPUT_TEXTAREA,
'hint' => 'hint about field',
],
'type' => [
'type' => ActiveFormBuilder::INPUT_DROPDOWN_LIST,
'items' => [1 => 'One', 2 => 'Two'],
'options' => [
'prompt' => 'select',
],
],
'published' => [
'type' => ActiveFormBuilder::INPUT_CHECKBOX,
],
'redactor' => [
'type' => ActiveFormBuilder::INPUT_WIDGET,
'widgetClass' => \vova07\imperavi\Widget::className(),
],
'raw_data' => [ // need to define attribute `raw_data` in model
'type' => ActiveFormBuilder::INPUT_RAW,
'value' => 'raw html data',
],
];
}
}
Now in form view you can write something like this:, (*9)
.....
<?php $form = \metalguardian\formBuilder\ActiveFormBuilder::begin(); ?>
<?= $form->renderForm($model, $model->getFormConfig()) ?>
<div class="form-group">
<?= Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success']) ?>
</div>
<?php \metalguardian\formBuilder\ActiveFormBuilder::end(); ?>
.....
Advanced Usage
You can define configuration of different elements in model, (*10)
<?php
namespace app\helpers;
use metalguardian\formBuilder\ActiveFormBuilder;
/**
*/
class Helper
{
.....
/**
* @return array
*/
public static function getLabelConfig()
{
return [
'type' => ActiveFormBuilder::INPUT_TEXT,
];
}
/**
* @return array
*/
public static function getContentConfig()
{
return [
'type' => ActiveFormBuilder::INPUT_TEXTAREA,
];
}
}
Now you can use different models in one form, (*11)
.....
<?php $form = \metalguardian\formBuilder\ActiveFormBuilder::begin(); ?>
<?= $form->renderField($model1, 'label', \app\helpers\Helper::getLabelConfig()); ?>
<?= $form->renderField($model1, 'content', \app\helpers\Helper::getContentConfig()); ?>
<?= $form->renderField($model2, 'label', \app\helpers\Helper::getLabelConfig()); ?>
<?= $form->renderField($model2, 'content', \app\helpers\Helper::getContentConfig()); ?>
<div class="form-group">
<?= Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success']) ?>
</div>
<?php \metalguardian\formBuilder\ActiveFormBuilder::end(); ?>
.....