PHP code example of evgeniydev / yii2-form-creator-behavior
1. Go to this page and download the library: Download evgeniydev/yii2-form-creator-behavior library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
evgeniydev / yii2-form-creator-behavior example snippets
...
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
],
]
...
];
}
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,
]);
}
...
'field1' => [
'type' => FormCreatorBehavior::CHECKBOX_TYPE,
'inputOptions' => [
'class' => 'someClass',
// other html options
],
],
'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
],
'class' => FormCreatorBehavior::className(),
'attributes' => [...], // array attributes
'wrapperBlockButtonsOptions' => [
'tag' => 'div', // tag name wrapper buttons block or false
'htmlOptions' => [...], // cancel button html options
],
'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
],
],
<?= $model->form;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.