Skip to content

Commit 9a200d4

Browse files
author
Eugene Tupikov
committed
Update doc
1 parent 24ce5d9 commit 9a200d4

File tree

4 files changed

+117
-4
lines changed

4 files changed

+117
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function($data) { return 'something'; }
109109
Widget supports several use cases:
110110

111111
- [Single column example](docs/single_column.md)
112-
- [Several columns example](docs/several_columns.md)
112+
- [Multiple columns example](docs/multiple_columns.md)
113113

114114
You cad find source code of examples [here](./examples/)
115115
##License

docs/images/multiple-column.gif

125 KB
Loading

docs/multiple_columns.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#Multiple columns example
2+
3+
![Multiple columns example](./images/multiple-column.gif?raw=true)
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+
```

docs/several_columns.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)