├── LICENSE ├── README.md ├── composer.json └── src └── ActiveFormBuilder.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Ivan Pushkin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Yii2 Form Builder 2 | ================= 3 | Small and easy form builder. You can store form configuration in form model. 4 | 5 | [![Latest Stable Version](https://poser.pugx.org/metalguardian/yii2-form-builder/v/stable.svg)](https://packagist.org/packages/metalguardian/yii2-form-builder) 6 | [![Total Downloads](https://poser.pugx.org/metalguardian/yii2-form-builder/downloads.svg)](https://packagist.org/packages/metalguardian/yii2-form-builder) 7 | [![Latest Unstable Version](https://poser.pugx.org/metalguardian/yii2-form-builder/v/unstable.svg)](https://packagist.org/packages/metalguardian/yii2-form-builder) 8 | [![License](https://poser.pugx.org/metalguardian/yii2-form-builder/license.svg)](https://packagist.org/packages/metalguardian/yii2-form-builder) 9 | 10 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/MetalGuardian/yii2-form-builder/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/MetalGuardian/yii2-form-builder/?branch=master) 11 | [![Code Coverage](https://scrutinizer-ci.com/g/MetalGuardian/yii2-form-builder/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/MetalGuardian/yii2-form-builder/?branch=master) 12 | [![Build Status](https://travis-ci.org/MetalGuardian/yii2-form-builder.svg?branch=master)](https://travis-ci.org/MetalGuardian/yii2-form-builder) 13 | [![Code Climate](https://codeclimate.com/github/MetalGuardian/yii2-form-builder/badges/gpa.svg)](https://codeclimate.com/github/MetalGuardian/yii2-form-builder) 14 | 15 | Installation 16 | ------------ 17 | 18 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/). 19 | 20 | Either run 21 | 22 | ``` 23 | php composer.phar require --prefer-dist metalguardian/yii2-form-builder "~1.0" 24 | ``` 25 | 26 | or add 27 | 28 | ``` 29 | "metalguardian/yii2-form-builder": "~1.0" 30 | ``` 31 | 32 | to the require section of your `composer.json` file. 33 | 34 | 35 | Usage 36 | ----- 37 | 38 | First of all you need write form config (you can store it in model class) 39 | 40 | [ 58 | 'type' => ActiveFormBuilder::INPUT_TEXT, 59 | ], 60 | 'content' => [ 61 | 'type' => ActiveFormBuilder::INPUT_TEXTAREA, 62 | 'hint' => 'hint about field', 63 | ], 64 | 'type' => [ 65 | 'type' => ActiveFormBuilder::INPUT_DROPDOWN_LIST, 66 | 'items' => [1 => 'One', 2 => 'Two'], 67 | 'options' => [ 68 | 'prompt' => 'select', 69 | ], 70 | ], 71 | 'published' => [ 72 | 'type' => ActiveFormBuilder::INPUT_CHECKBOX, 73 | ], 74 | 'redactor' => [ 75 | 'type' => ActiveFormBuilder::INPUT_WIDGET, 76 | 'widgetClass' => \vova07\imperavi\Widget::className(), 77 | ], 78 | 'raw_data' => [ // need to define attribute `raw_data` in model 79 | 'type' => ActiveFormBuilder::INPUT_RAW, 80 | 'value' => 'raw html data', 81 | ], 82 | ]; 83 | } 84 | } 85 | 86 | Now in form view you can write something like this: 87 | 88 | ..... 89 | 90 | 91 | 92 | renderForm($model, $model->getFormConfig()) ?> 93 | 94 |
95 | 'btn btn-success']) ?> 96 |
97 | 98 | 99 | ..... 100 | 101 | Advanced Usage 102 | -------------- 103 | 104 | You can define configuration of different elements in model 105 | 106 | ActiveFormBuilder::INPUT_TEXT, 124 | ]; 125 | } 126 | 127 | /** 128 | * @return array 129 | */ 130 | public static function getContentConfig() 131 | { 132 | return [ 133 | 'type' => ActiveFormBuilder::INPUT_TEXTAREA, 134 | ]; 135 | } 136 | } 137 | 138 | Now you can use different models in one form 139 | 140 | ..... 141 | 142 | 143 | renderField($model1, 'label', \app\helpers\Helper::getLabelConfig()); ?> 144 | renderField($model1, 'content', \app\helpers\Helper::getContentConfig()); ?> 145 | 146 | renderField($model2, 'label', \app\helpers\Helper::getLabelConfig()); ?> 147 | renderField($model2, 'content', \app\helpers\Helper::getContentConfig()); ?> 148 | 149 |
150 | 'btn btn-success']) ?> 151 |
152 | 153 | 154 | ..... 155 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "metalguardian/yii2-form-builder", 3 | "description": "Small and easy form builder", 4 | "type": "yii2-extension", 5 | "keywords": ["yii2","extension","form","builder"], 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Ivan Pushkin", 10 | "email": "imetalguardi+yii2@gmail.com" 11 | } 12 | ], 13 | "require": { 14 | "yiisoft/yii2": "*" 15 | }, 16 | "autoload": { 17 | "psr-4": { 18 | "metalguardian\\formBuilder\\": "src" 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/ActiveFormBuilder.php: -------------------------------------------------------------------------------- 1 | $options) { 46 | $form .= $this->renderField($model, $attribute, $options); 47 | } 48 | 49 | return $form; 50 | } 51 | 52 | 53 | /** 54 | * @param string $attribute 55 | * @param array $settings 56 | * @param Model $model 57 | * 58 | * @return ActiveField 59 | * @throws InvalidConfigException 60 | */ 61 | public function renderField(Model $model, $attribute, array $settings = []) 62 | { 63 | $fieldOptions = ArrayHelper::getValue($settings, 'fieldOptions', []); 64 | $field = $this->field($model, $attribute, $fieldOptions); 65 | 66 | if (($label = ArrayHelper::getValue($settings, 'label')) !== null) { 67 | $field->label($label, ArrayHelper::getValue($settings, 'labelOptions', [])); 68 | } 69 | if (($hint = ArrayHelper::getValue($settings, 'hint')) !== null) { 70 | $field->hint($hint, ArrayHelper::getValue($settings, 'hintOptions', [])); 71 | } 72 | 73 | $type = ArrayHelper::getValue($settings, 'type', static::INPUT_TEXT); 74 | $this->prepareField($field, $type, $settings); 75 | 76 | return $field; 77 | } 78 | 79 | /** 80 | * @param ActiveField $field 81 | * @param $type 82 | * @param array $settings 83 | * @throws InvalidConfigException 84 | */ 85 | protected function prepareField($field, $type, array $settings) 86 | { 87 | $options = ArrayHelper::getValue($settings, 'options', []); 88 | switch ($type) { 89 | case static::INPUT_HIDDEN: 90 | case static::INPUT_TEXT: 91 | case static::INPUT_TEXTAREA: 92 | case static::INPUT_PASSWORD: 93 | case static::INPUT_FILE: 94 | $field->$type($options); 95 | break; 96 | 97 | case static::INPUT_DROPDOWN_LIST: 98 | case static::INPUT_LIST_BOX: 99 | case static::INPUT_CHECKBOX_LIST: 100 | case static::INPUT_RADIO_LIST: 101 | $items = ArrayHelper::getValue($settings, 'items', []); 102 | $field->$type($items, $options); 103 | break; 104 | 105 | case static::INPUT_CHECKBOX: 106 | case static::INPUT_RADIO: 107 | $enclosedByLabel = ArrayHelper::getValue($settings, 'enclosedByLabel', true); 108 | $field->$type($options, $enclosedByLabel); 109 | break; 110 | 111 | case static::INPUT_HTML5: 112 | $html5type = ArrayHelper::getValue($settings, 'html5type', 'text'); 113 | $field->$type($html5type, $options); 114 | break; 115 | 116 | case static::INPUT_WIDGET: 117 | $widgetClass = $this->getWidgetClass($settings); 118 | $field->$type($widgetClass, $options); 119 | break; 120 | 121 | case static::INPUT_RAW: 122 | $field->parts['{input}'] = $this->getValue($settings); 123 | break; 124 | 125 | default: 126 | throw new InvalidConfigException("Invalid input type '{$type}' configured for the attribute."); 127 | } 128 | } 129 | 130 | /** 131 | * @param array $settings 132 | * @return mixed 133 | * @throws InvalidConfigException 134 | */ 135 | protected function getWidgetClass(array $settings) 136 | { 137 | $widgetClass = ArrayHelper::getValue($settings, 'widgetClass'); 138 | if (empty($widgetClass) && !$widgetClass instanceof InputWidget) { 139 | throw new InvalidConfigException( 140 | "A valid 'widgetClass' must be setup and extend from '\\yii\\widgets\\InputWidget'." 141 | ); 142 | } 143 | return $widgetClass; 144 | } 145 | 146 | /** 147 | * @param array $settings 148 | * @return mixed|string 149 | */ 150 | protected function getValue(array $settings) 151 | { 152 | $value = ArrayHelper::getValue($settings, 'value', ''); 153 | if (is_callable($value)) { 154 | return call_user_func($value); 155 | } elseif (!is_string($value)) { 156 | return ''; 157 | } 158 | return $value; 159 | } 160 | } 161 | --------------------------------------------------------------------------------