Skip to content

Commit 156e38f

Browse files
committed
Support of client validation
1 parent 3c8d88d commit 156e38f

File tree

13 files changed

+164
-29
lines changed

13 files changed

+164
-29
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
Yii2 multiple input change log
22
==============================
33

4+
2.1.0
5+
=====
6+
7+
- Enh #37: Support of client validation
8+
49
2.0.1
510
=====
611

7-
- Bug #105: Change vendor name in namespace from yii to unclead
12+
- Bug #105: Change vendor name in namespace from yii to unclead to respect Yii recommendations
813

914
2.0.0
1015
=====

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Yii2 widget for handle multiple inputs for an attribute of model and tabular inp
88
[![License](https://poser.pugx.org/unclead/yii2-multiple-input/license)](https://packagist.org/packages/unclead/yii2-multiple-input)
99

1010
##Latest release
11-
The latest stable version of the extension is v2.0.1 Follow the [instruction](./UPGRADE.md) for upgrading from previous versions
11+
The latest stable version of the extension is v2.1.0 Follow the [instruction](./UPGRADE.md) for upgrading from previous versions
1212

1313
##Installation
1414
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"yii2 tabular input"
1010
],
1111
"type": "yii2-extension",
12-
"version": "2.0.1",
12+
"version": "2.1.0",
1313
"license": "BSD-3-Clause",
1414
"support": {
1515
"issues": "https://github.com/unclead/yii2-multiple-input/issues?state=open",

docs/tips.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [Use of a widget's placeholder](#using-placeholder)
88
- [Custom index of the row](#custom-index)
99
- [Embedded MultipleInput widget](#embedded)
10+
- [Client validation](#client-validation)
1011

1112
##How to customize buttons
1213

@@ -182,3 +183,49 @@ echo MultipleInput::widget([
182183
```
183184

184185
But in this case you have to pass `attributeOptions` to the widget otherwise you will not be able to use ajax or client side validation of data.
186+
187+
### Client validation
188+
189+
Apart of ajax validation you can use client validation but in this case you MUST set property `form`.
190+
Also ensure that you set `enableClientValidation` to `true` value in property `attributeOptions`. If you want to use client validation
191+
for particular column you can use `attributeOptions` property for this column. An example of using client validation is listed below:
192+
193+
```php
194+
<?= TabularInput::widget([
195+
'models' => $models,
196+
'form' => $form,
197+
'attributeOptions' => [
198+
'enableAjaxValidation' => true,
199+
'enableClientValidation' => false,
200+
'validateOnChange' => false,
201+
'validateOnSubmit' => true,
202+
'validateOnBlur' => false,
203+
],
204+
'columns' => [
205+
[
206+
'name' => 'id',
207+
'type' => TabularColumn::TYPE_HIDDEN_INPUT
208+
],
209+
[
210+
'name' => 'title',
211+
'title' => 'Title',
212+
'type' => TabularColumn::TYPE_TEXT_INPUT,
213+
'attributeOptions' => [
214+
'enableClientValidation' => true,
215+
'validateOnChange' => true,
216+
],
217+
'enableError' => true
218+
],
219+
[
220+
'name' => 'description',
221+
'title' => 'Description',
222+
],
223+
],
224+
]) ?>
225+
226+
```
227+
228+
In the example above we use client validation for column `title` and ajax validation for column `description`.
229+
As you can noticed we also enabled `validateOnChange` for column `title` thus you can use all client-side options from the `ActiveField` class.
230+
231+

examples/models/ExampleModel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public function rules()
9393
{
9494
return [
9595
['title', 'required'],
96+
['title', 'string', 'min' => 5],
9697
['emails', 'validateEmails'],
9798
['phones', 'validatePhones'],
9899
['schedule', 'validateSchedule'],

examples/views/tabular-input.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
'validateOnSubmit' => true,
2828
'validateOnBlur' => false,
2929
],
30+
'form' => $form,
3031
'columns' => [
3132
[
3233
'name' => 'id',
@@ -36,6 +37,10 @@
3637
'name' => 'title',
3738
'title' => 'Title',
3839
'type' => TabularColumn::TYPE_TEXT_INPUT,
40+
'attributeOptions' => [
41+
'enableClientValidation' => true,
42+
'validateOnChange' => true,
43+
],
3944
'defaultValue' => 'Test',
4045
'enableError' => true
4146
],

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jquery.multipleInput",
3-
"version": "2.0.1",
3+
"version": "2.1.0",
44
"description": "jQuery multipleInput",
55
"scripts": {
66
"build": "npm install && (gulp || node node_modules/gulp/bin/gulp.js)"

src/MultipleInput.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
namespace unclead\multipleinput;
1010

1111
use Yii;
12+
use yii\base\InvalidConfigException;
1213
use yii\base\Model;
1314
use yii\helpers\ArrayHelper;
15+
use yii\widgets\ActiveForm;
1416
use yii\widgets\InputWidget;
1517
use yii\db\ActiveRecordInterface;
1618
use unclead\multipleinput\renderers\TableRenderer;
@@ -114,13 +116,23 @@ class MultipleInput extends InputWidget
114116
*/
115117
public $isEmbedded;
116118

119+
/**
120+
* @var ActiveForm an instance of ActiveForm which you have to pass in case of using client validation
121+
* @since 2.1
122+
*/
123+
public $form;
124+
117125
/**
118126
* Initialization.
119127
*
120128
* @throws \yii\base\InvalidConfigException
121129
*/
122130
public function init()
123131
{
132+
if ($this->form !== null && !$this->form instanceof ActiveForm) {
133+
throw new InvalidConfigException('Property "form" must be an instance of yii\widgets\ActiveForm');
134+
}
135+
124136
$this->guessColumns();
125137
$this->initData();
126138

@@ -196,6 +208,7 @@ private function createRenderer()
196208
'addButtonPosition' => $this->addButtonPosition,
197209
'rowOptions' => $this->rowOptions,
198210
'context' => $this,
211+
'form' => $this->form
199212
];
200213

201214
if ($this->removeButtonOptions !== null) {

src/TabularInput.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use yii\base\Model;
1414
use yii\db\ActiveRecordInterface;
1515
use yii\bootstrap\Widget;
16+
use yii\widgets\ActiveForm;
1617
use unclead\multipleinput\renderers\TableRenderer;
1718
use unclead\multipleinput\renderers\RendererInterface;
1819

@@ -102,6 +103,12 @@ class TabularInput extends Widget
102103
*/
103104
public $rendererClass;
104105

106+
/**
107+
* @var ActiveForm an instance of ActiveForm which you have to pass in case of using client validation
108+
* @since 2.1
109+
*/
110+
public $form;
111+
105112
/**
106113
* Initialization.
107114
*
@@ -113,6 +120,10 @@ public function init()
113120
throw new InvalidConfigException('You must specify "models"');
114121
}
115122

123+
if ($this->form !== null && !$this->form instanceof ActiveForm) {
124+
throw new InvalidConfigException('Property "form" must be an instance of yii\widgets\ActiveForm');
125+
}
126+
116127
foreach ($this->models as $model) {
117128
if (!$model instanceof Model) {
118129
throw new InvalidConfigException('Model has to be an instance of yii\base\Model');
@@ -147,6 +158,7 @@ private function createRenderer()
147158
'rowOptions' => $this->rowOptions,
148159
'addButtonPosition' => $this->addButtonPosition,
149160
'context' => $this,
161+
'form' => $this->form
150162
];
151163

152164
if ($this->removeButtonOptions !== null) {

src/assets/src/js/jquery.multipleInput.js

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,8 @@
5757
max: 1,
5858
// minimum number of rows
5959
min: 1,
60-
attributeOptions: {},
61-
indexPlaceholder: 'multiple_index',
62-
};
63-
64-
var defaultAttributeOptions = {
65-
enableAjaxValidation: false,
66-
validateOnBlur: false,
67-
validateOnChange: false,
68-
validateOnType: false
60+
attributes: {},
61+
indexPlaceholder: 'multiple_index'
6962
};
7063

7164
var isActiveFormEnabled = false;
@@ -79,8 +72,7 @@
7972

8073
$wrapper.data('multipleInput', {
8174
settings: settings,
82-
currentIndex: 0,
83-
attributeDefaults: {}
75+
currentIndex: 0
8476
});
8577

8678

@@ -100,27 +92,32 @@
10092
var intervalID = setInterval(function () {
10193
if (typeof form.data('yiiActiveForm') === 'object') {
10294
var attribute = form.yiiActiveForm('find', id),
103-
attributeDefaults = [];
104-
95+
defaultAttributeOptions = {
96+
enableAjaxValidation: false,
97+
validateOnBlur: false,
98+
validateOnChange: false,
99+
validateOnType: false,
100+
validationDelay: 500
101+
};
102+
103+
// fetch default attribute options from active from attribute
105104
if (typeof attribute === 'object') {
106105
$.each(attribute, function (key, value) {
107106
if (['id', 'input', 'container'].indexOf(key) == -1) {
108-
attributeDefaults[key] = value;
107+
defaultAttributeOptions[key] = value;
109108
}
110109
});
111110

112111
form.yiiActiveForm('remove', id);
113112
}
114113

115-
var attributeOptions = $.extend({}, defaultAttributeOptions, settings.attributeOptions);
116-
117-
$.each(attributeOptions, function (key, value) {
118-
if (typeof attributeDefaults[key] === 'undefined') {
119-
attributeDefaults[key] = value;
120-
}
114+
// append default options to option from settings
115+
$.each(settings.attributes, function (attribute, attributeOptions) {
116+
attributeOptions = $.extend({}, defaultAttributeOptions, attributeOptions);
117+
settings.attributes[attribute] = attributeOptions;
121118
});
122119

123-
$wrapper.data('multipleInput').attributeDefaults = attributeDefaults;
120+
$wrapper.data('multipleInput').settings = settings;
124121

125122
$wrapper.find('.multiple-input-list').find('input, select, textarea').each(function () {
126123
addAttribute($(this));
@@ -318,9 +315,10 @@
318315
return;
319316
}
320317

321-
322318
var data = wrapper.data('multipleInput');
323-
form.yiiActiveForm('add', $.extend({}, data.attributeDefaults, {
319+
var bareID = id.replace(/-\d/, '').replace(/-\d-/, '');
320+
321+
form.yiiActiveForm('add', $.extend({}, data.settings.attributes[bareID], {
324322
'id': id,
325323
'input': '#' + id,
326324
'container': '.field-' + id

src/assets/src/js/jquery.multipleInput.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/BaseColumn.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@ abstract class BaseColumn extends Object
110110
* @var mixed the context of using a column. It is an instance of widget(MultipleInput or TabularInput).
111111
*/
112112
public $context;
113+
114+
/**
115+
* @var array client-side options of the attribute, e.g. enableAjaxValidation.
116+
* You can use this property for custom configuration of the column (attribute).
117+
* By default, the column will use options which are defined on widget level.
118+
*
119+
* @since 2.1
120+
*/
121+
public $attributeOptions = [];
113122

114123
/**
115124
* @var Model|ActiveRecordInterface|array

0 commit comments

Comments
 (0)