Skip to content

Commit ae7bcd8

Browse files
author
Eugene Tupikov
committed
Improvements
1 parent 5e62afc commit ae7bcd8

File tree

4 files changed

+54
-52
lines changed

4 files changed

+54
-52
lines changed

MultipleInput.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ public function init()
6969
*/
7070
public function run()
7171
{
72-
echo Html::beginTag('div', [
73-
'class' => 'list-group list-group-' . $this->getId(),
74-
]);
72+
echo Html::beginTag('div', ['id' => $this->getId(), 'class' => 'list-group']);
7573
echo Html::beginTag('table', [
7674
'class' => 'multiple-input-list table table-condensed'
7775
]);
@@ -229,7 +227,7 @@ private function getRowTemplate()
229227
*/
230228
private function renderRow($index, $data = null)
231229
{
232-
$btnAction = $index == 0 ? self::ACTION_ADD : self::ACTION_REMOVE;
230+
$btnAction = $index == 0 ? self::ACTION_ADD : self::ACTION_REMOVE;
233231
$btnType = $index == 0 ? Button::TYPE_DEFAULT : Button::TYPE_DANGER;
234232

235233
$search = ['{index}', '{btn_action}', '{btn_type}'];
@@ -267,14 +265,18 @@ private function prepareColumnValue($column, $data)
267265

268266
/**
269267
* @param $name
268+
* @param string $index
270269
* @return string
271270
*/
272-
private function getElementName($name)
271+
private function getElementName($name, $index = null)
273272
{
274273
$elementName = $this->getName();
274+
if ($index === null) {
275+
$index = '{index}';
276+
}
275277
$elementName .= count($this->getColumns()) > 1
276-
? '[{index}][' . $name . ']'
277-
: '[' . $name . '][{index}]';
278+
? '[' . $index . '][' . $name . ']'
279+
: '[' . $name . '][' . $index . ']';
278280
return $elementName;
279281
}
280282

@@ -328,7 +330,7 @@ public function registerClientScript()
328330
MultipleInputAsset::register($view);
329331
$options = Json::encode(
330332
[
331-
'group_id' => $this->getId(),
333+
'id' => $this->getId(),
332334
'template' => $this->getRowTemplate(),
333335
'btn_action' => self::ACTION_REMOVE,
334336
'btn_type' => Button::TYPE_DANGER,

assets/src/js/jquery.multipleInput.js

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,44 @@
1111
};
1212

1313
var defaultOptions = {
14-
group_id: null,
14+
id: null,
1515
template: null,
1616
btn_action: null,
1717
btn_type: null,
1818
limit: 1,
1919
replacement: []
2020
};
2121

22+
var attributeDefaults = {};
23+
2224
var methods = {
2325
init: function (options) {
2426
var settings = $.extend(defaultOptions, options || {});
2527

26-
$(document).on('click.multipleinput', '.js-' + settings.group_id + '-input-remove', function (e) {
28+
$(document).on('click.multipleinput', '.js-' + settings.id + '-input-remove', function (e) {
2729
e.preventDefault();
2830
methods.removeInput.apply(this);
2931
});
3032

31-
$(document).on('click.multipleinput', '.js-'+ settings.group_id + '-input-plus', function (e) {
33+
$(document).on('click.multipleinput', '.js-'+ settings.id + '-input-plus', function (e) {
3234
e.preventDefault();
3335
methods.addInput.apply(this,[settings]);
3436
});
3537

36-
$(function() {
37-
$('.multiple-input-list').find('input, select, textarea').each(function () {
38+
var wrapper = $('#' + settings.id),
39+
form = wrapper.closest('form');
40+
41+
setTimeout(function() {
42+
var attributes = form.data('yiiActiveForm').attributes;
43+
$.each(attributes[0], function(key, value) {
44+
attributeDefaults[key] = value;
45+
});
46+
form.data('yiiActiveForm').attributes = [];
47+
48+
wrapper.find('.multiple-input-list').find('input, select, textarea').each(function () {
3849
methods.addAttribute.apply(this);
3950
});
40-
});
51+
}, 100);
4152
},
4253

4354
addInput: function (settings) {
@@ -51,7 +62,6 @@
5162
if (settings.limit != null && index >= settings.limit) {
5263
return;
5364
}
54-
5565
var search = ['{index}', '{btn_action}', '{btn_type}', '{value}'],
5666
replace = [index, btn_action, btn_type, ''];
5767

@@ -63,7 +73,7 @@
6373
template = template.replaceAll('{' + j + '}', replacement[j]);
6474
}
6575

66-
console.log(template);
76+
6777
$(template).hide().appendTo($wrapper).fadeIn(300);
6878
$(template).find('input, select, textarea').each(function () {
6979
methods.addAttribute.apply(this);
@@ -81,40 +91,22 @@
8191
},
8292

8393
addAttribute: function () {
84-
var id = $(this).attr('id');
85-
var list = methods.getAttributesList.apply(this);
86-
var isExists = false;
87-
88-
for (var i in list) {
89-
if (list[i]['id'] == id) {
90-
isExists = true;
91-
}
92-
}
93-
if (!isExists) {
94-
list.push({
95-
'id': id,
96-
'input': '#' + id,
97-
'container': '.field-' + id,
98-
'enableAjaxValidation': true
99-
});
100-
}
94+
var id = $(this).attr('id'),
95+
form = $('#' + $(this).attr('id')).closest('form');
96+
97+
form.yiiActiveForm('add', $.extend(attributeDefaults, {
98+
'id': id,
99+
'input': '#' + id,
100+
'container': '.field-' + id
101+
}));
101102
},
102103

103104
removeAttribute: function () {
104105
var id = $(this).attr('id');
105-
var list = methods.getAttributesList.apply(this);
106-
for (var i in list) {
107-
if (list[i]['id'] == id) {
108-
delete list[id];
109-
return;
110-
}
111-
}
112-
},
113-
114-
getAttributesList: function () {
115106
var form = $('#' + $(this).attr('id')).closest('form');
116-
return form.data('yiiActiveForm')['attributes'];
107+
form.yiiActiveForm('remove', id);
117108
}
109+
118110
};
119111

120112
String.prototype.replaceAll = function(search, replace){

examples/actions/MultipleInputAction.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Yii;
66
use yii\base\Action;
7+
use yii\bootstrap\ActiveForm;
8+
use yii\web\Response;
79
use unclead\widgets\examples\models\ExampleModel;
810

911
/**
@@ -15,12 +17,17 @@ class MultipleInputAction extends Action
1517
public function run()
1618
{
1719
Yii::setAlias('@unclead-examples', realpath(__DIR__ . '/../'));
20+
1821
$model = new ExampleModel();
1922

20-
if ($model->load(Yii::$app->request->post())) {
21-
if (!$model->validate()) {
22-
var_dump($model->getErrors());
23-
}
23+
$request = Yii::$app->getRequest();
24+
if ($request->isPost && $request->post('ajax') !== null) {
25+
$model->load(Yii::$app->request->post());
26+
Yii::$app->response->format = Response::FORMAT_JSON;
27+
return ActiveForm::validate($model);
28+
}
29+
30+
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
2431

2532
}
2633
return $this->controller->render('@unclead-examples/views/example.php', ['model' => $model]);

examples/views/example.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
?>
1111

1212
<?php $form = ActiveForm::begin([
13-
'enableAjaxValidation' => false,
13+
'enableAjaxValidation' => true,
1414
'enableClientValidation' => false,
1515
'validateOnChange' => false,
1616
'validateOnSubmit' => true,
@@ -21,9 +21,10 @@
2121
'limit' => 4,
2222
]);
2323
?>
24-
<?= $form->field($model, 'phones')->widget(MultipleInput::className(), [
25-
'limit' => 4,
26-
]);
24+
<?php
25+
// echo $form->field($model, 'phones')->widget(MultipleInput::className(), [
26+
// 'limit' => 4,
27+
// ]);
2728
?>
2829
<?= Button::update();?>
2930
<?php ActiveForm::end();?>

0 commit comments

Comments
 (0)