evgeniydev / yii2-form-creator-behavior
用于创建视图表单的Yii2行为
1.0
2017-12-01 15:56 UTC
Requires
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-09-26 17:03:17 UTC
README
这是一个用于为CREATE和UPDATE方法生成视图表单的行为。
安装
安装此扩展的首选方法是通过 composer。
运行
php composer.phar require evgeniydev/yii2-form-creator-behavior
或者将以下内容添加到您的 composer.json
文件的require部分。
"evgeniydev/yii2-form-creator-behavior": "*"
基本用法
模型文件(app/models/SomeModel.php)
... 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 ], ] ... ]; }
控制器文件(app/controllers/SomeController.php)
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, ]); } ...
视图文件(app/views/some/form.php)
<?= $model->form; ?>
其他字段类型
复选框
'field1' => [ 'type' => FormCreatorBehavior::CHECKBOX_TYPE, 'inputOptions' => [ 'class' => 'someClass', // other html options ], ],
复选框列表
'field1' => [ 'type' => FormCreatorBehavior::CHECKBOXLIST_TYPE, 'items' => ['check1', 'check2'], 'inputOptions' => [ 'class' => 'someClass', // other html options ], ],
小部件
'field1' => [ 'type' => FormCreatorBehavior::WIDGET_TYPE, 'widgetClass' => '' // widget class name 'widgetOptions' => [...], // widget options ],
下拉框
'field1' => [ 'type' => FormCreatorBehavior::LISTBOX_TYPE, 'items' => ['check1', 'check2'], 'inputOptions' => [ 'class' => 'someClass', // other html options ], ],
自定义类型的输入框
'field1' => [ // <input type="tel"> 'type' => FormCreatorBehavior::INPUT_TYPE, 'inputType' => 'tel', 'inputOptions' => [ 'class' => 'someClass', // other html options ], ],
完整类型常量列表
- TEXT_INPUT_TYPE - 文本输入
- TEXTAREA_TYPE - 多行文本框
- CHECKBOX_TYPE - 复选框
- DROPDOWNLIST_TYPE - 下拉列表
- WIDGET_TYPE - 小部件
- INPUT_TYPE - 自定义类型的输入框
- HIDDEN_INPUT_TYPE - 隐藏输入框
- FILE_INPUT_TYPE - 文件输入框
- PASSWORD_INPUT_TYPE - 密码输入框
- RADIO_TYPE - 单选按钮
- RADIOLIST_TYPE - 单选列表
- CHECKBOXLIST_TYPE - 复选列表
- LISTBOX_TYPE - 列表框
其他配置
可调用的函数输入 函数模板:function($form, $model){ return ...; }
'field1' => function($form, $model) { // callable function return $form->field($model, 'field1'); },
标签
'field1' => [ 'type' => FormCreatorBehavior::TEXT_INPUT_TYPE, 'label' => 'Some label', // or false, if want not show label ... ],
提示(简短形式)
'field1' => [ 'type' => FormCreatorBehavior::TEXT_INPUT_TYPE, 'hint' => 'Some hint', ... ],
或完整形式
'field1' => [ 'type' => FormCreatorBehavior::TEXT_INPUT_TYPE, 'hint' => [ 'content' => 'Some content', // hint content 'options' => [...], // hint options ], ... ],
表单配置
'class' => FormCreatorBehavior::className(), 'attributes' => [...], // array attributes 'formOptions' => [ 'options' => [ 'enctype' => 'multipart/form-data' ], 'id' => 'Some id', ... // other \yii\widgets\ActiveForm widget options ]
模板视图
'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
提交按钮选项
'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 ], ],
取消按钮选项
'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 ], ],