|
| 1 | +#Multiple columns example |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +For example you want to have an interface for manage user schedule. For simpliсшен we will store the schedule in json string. |
| 6 | + |
| 7 | +In this case you can use `yii2-multiple-input` widget for supporting multiple inputs how to describe below. |
| 8 | + |
| 9 | +Our test model can looks like as the following snippet |
| 10 | + |
| 11 | +```php |
| 12 | +class ExampleModel extends Model |
| 13 | +{ |
| 14 | + public $schedule; |
| 15 | + |
| 16 | + public function init() |
| 17 | + { |
| 18 | + parent::init(); |
| 19 | + |
| 20 | + $this->schedule = [ |
| 21 | + [ |
| 22 | + 'day' => 0, |
| 23 | + 'user_id' => 1, |
| 24 | + 'priority' => 1 |
| 25 | + ], |
| 26 | + [ |
| 27 | + 'day' => 0, |
| 28 | + 'user_id' => 2, |
| 29 | + 'priority' => 2 |
| 30 | + ], |
| 31 | + ]; |
| 32 | + } |
| 33 | +``` |
| 34 | + |
| 35 | +Then we have to use `MultipleInput` widget for rendering form field in the view file |
| 36 | + |
| 37 | +```php |
| 38 | +use yii\bootstrap\ActiveForm; |
| 39 | +use unclead\widgets\MultipleInput; |
| 40 | +use unclead\widgets\examples\models\ExampleModel; |
| 41 | +use yii\helpers\Html; |
| 42 | + |
| 43 | +/* @var $this \yii\base\View */ |
| 44 | +/* @var $model ExampleModel */ |
| 45 | +?> |
| 46 | + |
| 47 | +<?php $form = ActiveForm::begin([ |
| 48 | + 'enableAjaxValidation' => true, |
| 49 | + 'enableClientValidation' => false, |
| 50 | + 'validateOnChange' => false, |
| 51 | + 'validateOnSubmit' => true, |
| 52 | + 'validateOnBlur' => false, |
| 53 | +]);?> |
| 54 | + |
| 55 | +<?= $form->field($model, 'schedule')->widget(MultipleInput::className(), [ |
| 56 | + 'limit' => 4, |
| 57 | + 'columns' => [ |
| 58 | + [ |
| 59 | + 'name' => 'user_id', |
| 60 | + 'type' => 'dropDownList', |
| 61 | + 'title' => 'User', |
| 62 | + 'defaultValue' => 1, |
| 63 | + 'items' => [ |
| 64 | + 1 => 'User 1', |
| 65 | + 2 => 'User 2' |
| 66 | + ] |
| 67 | + ], |
| 68 | + [ |
| 69 | + 'name' => 'day', |
| 70 | + 'type' => 'dropDownList', |
| 71 | + 'title' => 'Day', |
| 72 | + 'value' => function($data) { |
| 73 | + return $data['day']; |
| 74 | + }, |
| 75 | + 'defaultValue' => 1, |
| 76 | + 'items' => [ |
| 77 | + '0' => 'Saturday', |
| 78 | + '1' => 'Monday' |
| 79 | + ], |
| 80 | + 'options' => [ |
| 81 | + |
| 82 | + ] |
| 83 | + ], |
| 84 | + [ |
| 85 | + 'name' => 'priority', |
| 86 | + 'title' => 'Priority', |
| 87 | + 'options' => [ |
| 88 | + 'class' => 'input-priority' |
| 89 | + ] |
| 90 | + ] |
| 91 | + ] |
| 92 | + ]); |
| 93 | +?> |
| 94 | +<?= Html::submitButton('Update', ['class' => 'btn btn-success']);?> |
| 95 | +<?php ActiveForm::end();?> |
| 96 | +``` |
| 97 | + |
| 98 | + |
| 99 | +For validation the schedule you can use the following code |
| 100 | + |
| 101 | +```php |
| 102 | + |
| 103 | + public function validateSchedule($attribute) |
| 104 | + { |
| 105 | + $requiredValidator = new RequiredValidator(); |
| 106 | + |
| 107 | + foreach($this->$attribute as $index => $row) { |
| 108 | + $error = null; |
| 109 | + $requiredValidator->validate($row['priority'], $error); |
| 110 | + if (!empty($error)) { |
| 111 | + $key = $attribute . '[' . $index . '][priority]'; |
| 112 | + $this->addError($key, $error); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +``` |
0 commit comments