Skip to content

Commit d08fe29

Browse files
committed
#37 Added support of client validation
1 parent 1d25dae commit d08fe29

16 files changed

+228
-112
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Yii2 multiple input change log
55
---------------------
66

77
- Bug #70: replacing of the placeholder after preparing the content of row
8+
- Enh #37: Added support of client validation
89

910
1.2.15
1011
------

README.md

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ Contents:
2020
- [Work with empty list](#work-with-empty-list)
2121
- [Guess column title](#guess-column-title)
2222
- [Ajax loading of a widget](#ajax-loading)
23-
- [Using of a placeholder {multiple_index}](#using-placeholder)
23+
- [Use of a widget's placeholder](#using-placeholder)
24+
- [How to enable client validation](#client-validation)
2425
- [Javascript Events](#javascript-events)
2526
- [Renderers](#renderers)
2627

@@ -119,6 +120,11 @@ function($data) {}
119120

120121
**errorOptions** *array*: the HTMl attributes for the error tag
121122

123+
**attributeOptions** *array*: client-side attribute options of the column.
124+
You can use it to configure a column in specific way, e.g. you can enable client validation for particular
125+
column meanwhile other columns will be validated via ajax validation
126+
127+
122128
### Input types
123129

124130
Each column in a row can has their own type. Widget supports:
@@ -371,9 +377,9 @@ Assume you want to load a widget via ajax and then show it inside modal window.
371377

372378
You can fina an example of usage in a discussion of [issue](https://github.com/unclead/yii2-multiple-input/issues/58)
373379

374-
### Using of a placeholder {multiple_index} <a id="using-placeholder"></a>
380+
### se of a widget's placeholder <a id="using-placeholder"></a>
375381

376-
You can use a placeholder {multiple index} in a widget configuration, e.g. for implementation of dependent drop down lists.
382+
You can use a placeholder `{multiple index}` in a widget configuration, e.g. for implementation of dependent drop down lists.
377383

378384
```php
379385
<?= $form->field($model, 'field')->widget(MultipleInput::className(), [
@@ -416,6 +422,51 @@ JS
416422
?>
417423
```
418424

425+
### How to enable client validation <a id="client-validation"></a>
426+
427+
Apart of ajax validation you can use client validation but in this case you MUST set property `form`.
428+
Also ensure that you set `enableClientValidation` to `true` value in property `attributeOptions`. If you want to use client validation
429+
for particular column you can use `attributeOptions` property for this column. An example of using client validation is listed below:
430+
431+
```php
432+
<?= TabularInput::widget([
433+
'models' => $models,
434+
'form' => $form,
435+
'attributeOptions' => [
436+
'enableAjaxValidation' => true,
437+
'enableClientValidation' => false,
438+
'validateOnChange' => false,
439+
'validateOnSubmit' => true,
440+
'validateOnBlur' => false,
441+
],
442+
'columns' => [
443+
[
444+
'name' => 'id',
445+
'type' => TabularColumn::TYPE_HIDDEN_INPUT
446+
],
447+
[
448+
'name' => 'title',
449+
'title' => 'Title',
450+
'type' => TabularColumn::TYPE_TEXT_INPUT,
451+
'attributeOptions' => [
452+
'enableClientValidation' => true,
453+
'validateOnChange' => true,
454+
],
455+
'enableError' => true
456+
],
457+
[
458+
'name' => 'description',
459+
'title' => 'Description',
460+
],
461+
],
462+
]) ?>
463+
464+
```
465+
466+
In the example above we use client validation for column `title` and ajax validation for column `description`.
467+
As you can noticed we also enabled `validateOnChange` for column `title` thus you can use all client-side options from the `ActiveField` class.
468+
469+
419470
## JavaScript events
420471
This widget has following events:
421472
- `afterInit`: triggered after initialization

examples/actions/MultipleInputAction.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ public function run()
3333
Yii::error('Validation errors: ' . print_r($model->getErrors(), true));
3434
}
3535
}
36-
37-
36+
3837
return $this->controller->render('@unclead-examples/views/multiple-input.php', ['model' => $model]);
3938
}
4039
}

examples/models/ExampleModel.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,25 @@ class ExampleModel extends Model
2222
public $emails;
2323

2424
/**
25-
* @var
25+
* @var array
2626
*/
2727
public $phones;
2828

2929
/**
30-
* @var
30+
* @var array
3131
*/
3232
public $schedule;
3333

34+
/**
35+
* @var bool
36+
*/
3437
public $enable;
3538

39+
/**
40+
* @var string
41+
*/
42+
public $title;
43+
3644
public function init()
3745
{
3846
parent::init();
@@ -62,6 +70,7 @@ public function init()
6270
public function rules()
6371
{
6472
return [
73+
['title', 'required'],
6574
['emails', 'validateEmails'],
6675
['phones', 'validatePhones'],
6776
['schedule', 'validateSchedule']
@@ -72,14 +81,16 @@ public function attributes()
7281
{
7382
return [
7483
'emails',
75-
'phones'
84+
'phones',
85+
'title',
86+
'schedule'
7687
];
7788
}
7889

7990
public function scenarios()
8091
{
8192
return [
82-
self::SCENARIO_DEFAULT => ['emails', 'phones', 'schedule']
93+
self::SCENARIO_DEFAULT => ['emails', 'phones', 'schedule', 'title']
8394
];
8495
}
8596

examples/models/Item.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function rules()
5050
{
5151
return [
5252
[['title', 'description'], 'required'],
53+
[['title'], 'string', 'min' => 5, 'max' => 64],
5354
[['id', 'file'], 'safe']
5455
];
5556
}

examples/views/multiple-input.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
]
134134
]
135135
]);
136+
136137
?>
137138

138139
<?= Html::submitButton('Update', ['class' => 'btn btn-success']);?>

examples/views/tabular-input.php

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,48 @@
33
use yii\bootstrap\ActiveForm;
44
use unclead\widgets\TabularInput;
55
use yii\helpers\Html;
6-
use \unclead\widgets\examples\models\Item;
6+
use unclead\widgets\examples\models\Item;
7+
use unclead\widgets\TabularColumn;
8+
79

810
/* @var $this \yii\web\View */
911
/* @var $models Item[] */
1012
?>
1113

1214
<?php $form = \yii\bootstrap\ActiveForm::begin([
13-
'id' => 'tabular-form',
14-
'enableAjaxValidation' => true,
15-
'enableClientValidation' => false,
16-
'validateOnChange' => false,
17-
'validateOnSubmit' => true,
18-
'validateOnBlur' => false,
15+
'id' => 'tabular-form',
1916
'options' => [
2017
'enctype' => 'multipart/form-data'
2118
]
2219
]) ?>
2320

2421
<?= TabularInput::widget([
2522
'models' => $models,
23+
'form' => $form,
2624
'attributeOptions' => [
27-
'enableAjaxValidation' => true,
28-
'enableClientValidation' => false,
29-
'validateOnChange' => false,
30-
'validateOnSubmit' => true,
31-
'validateOnBlur' => false,
25+
'enableAjaxValidation' => true,
26+
'enableClientValidation' => false,
27+
'validateOnChange' => false,
28+
'validateOnSubmit' => true,
29+
'validateOnBlur' => false,
3230
],
3331
'columns' => [
3432
[
3533
'name' => 'id',
36-
'type' => \unclead\widgets\TabularColumn::TYPE_HIDDEN_INPUT
34+
'type' => TabularColumn::TYPE_HIDDEN_INPUT
3735
],
3836
[
39-
'name' => 'title',
37+
'name' => 'title',
4038
'title' => 'Title',
41-
'type' => \unclead\widgets\MultipleInputColumn::TYPE_TEXT_INPUT,
39+
'type' => TabularColumn::TYPE_TEXT_INPUT,
40+
'attributeOptions' => [
41+
'enableClientValidation' => true,
42+
'validateOnChange' => true,
43+
],
44+
'enableError' => true
4245
],
4346
[
44-
'name' => 'description',
47+
'name' => 'description',
4548
'title' => 'Description',
4649
],
4750
// [
@@ -55,7 +58,7 @@
5558
// ]
5659
// ],
5760
[
58-
'name' => 'date',
61+
'name' => 'date',
5962
'type' => \kartik\date\DatePicker::className(),
6063
'title' => 'Day',
6164
'options' => [
@@ -73,5 +76,5 @@
7376
]) ?>
7477

7578

76-
<?= Html::submitButton('Update', ['class' => 'btn btn-success']);?>
77-
<?php ActiveForm::end();?>
79+
<?= Html::submitButton('Update', ['class' => 'btn btn-success']); ?>
80+
<?php ActiveForm::end(); ?>

src/MultipleInput.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
use Yii;
1212
use yii\base\Model;
13+
use yii\widgets\ActiveForm;
1314
use yii\widgets\InputWidget;
14-
use yii\db\ActiveRecord;
15+
use yii\db\ActiveRecordInterface;
1516
use unclead\widgets\renderers\TableRenderer;
1617

1718

@@ -26,7 +27,7 @@ class MultipleInput extends InputWidget
2627
const POS_ROW = 1;
2728

2829
/**
29-
* @var ActiveRecord[]|array[] input data
30+
* @var ActiveRecordInterface[]|array[] input data
3031
*/
3132
public $data = null;
3233

@@ -99,6 +100,11 @@ class MultipleInput extends InputWidget
99100
*/
100101
public $columnClass;
101102

103+
/**
104+
* @var ActiveForm the instance of `ActiveForm` class.
105+
*/
106+
public $form;
107+
102108
/**
103109
* Initialization.
104110
*
@@ -166,7 +172,8 @@ private function createRenderer()
166172
'min' => $this->min,
167173
'addButtonPosition' => $this->addButtonPosition,
168174
'rowOptions' => $this->rowOptions,
169-
'context' => $this
175+
'context' => $this,
176+
'form' => $this->form
170177
];
171178

172179
if (!is_null($this->removeButtonOptions)) {

src/MultipleInputColumn.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class MultipleInputColumn extends BaseColumn
2323
/**
2424
* @var MultipleInput
2525
*/
26-
public $widget;
26+
public $context;
2727

2828
/**
2929
* @throws InvalidConfigException
@@ -32,7 +32,7 @@ public function init()
3232
{
3333
parent::init();
3434

35-
if ($this->enableError && !$this->widget->model instanceof Model) {
35+
if ($this->enableError && !$this->context->model instanceof Model) {
3636
throw new InvalidConfigException('Property "enableError" available only when model is defined.');
3737
}
3838
}
@@ -63,19 +63,19 @@ public function getElementName($index, $withPrefix = true)
6363
*/
6464
private function getInputNamePrefix()
6565
{
66-
$model = $this->widget->model;
66+
$model = $this->context->model;
6767
if ($model instanceof Model) {
6868
if (empty($this->renderer->columns) || (count($this->renderer->columns) == 1 && $this->hasModelAttribute($this->name))) {
6969
return $model->formName();
7070
}
71-
return Html::getInputName($this->widget->model, $this->widget->attribute);
71+
return Html::getInputName($this->context->model, $this->context->attribute);
7272
}
73-
return $this->widget->name;
73+
return $this->context->name;
7474
}
7575

7676
private function hasModelAttribute($name)
7777
{
78-
$model = $this->widget->model;
78+
$model = $this->context->model;
7979

8080
if ($model->hasProperty($name)) {
8181
return true;
@@ -88,7 +88,7 @@ private function hasModelAttribute($name)
8888

8989
public function getFirstError($index)
9090
{
91-
$attribute = $this->widget->attribute . $this->getElementName($index, false);
92-
return $this->widget->model->getFirstError($attribute);
91+
$attribute = $this->context->attribute . $this->getElementName($index, false);
92+
return $this->context->model->getFirstError($attribute);
9393
}
9494
}

src/TabularColumn.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,4 @@ public function setModel($model)
7373
parent::setModel($model);
7474
}
7575
}
76-
77-
7876
}

0 commit comments

Comments
 (0)