Skip to content

Commit 031f971

Browse files
committed
Improving for better work without ActiveForm
1 parent 4cee01b commit 031f971

File tree

3 files changed

+81
-20
lines changed

3 files changed

+81
-20
lines changed

examples/views/embedded-input.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,25 @@
1717
'validateOnSubmit' => true,
1818
'validateOnBlur' => false,
1919
];
20+
21+
$enableActiveForm = false;
2022
?>
2123

22-
<?php $form = ActiveForm::begin([
23-
'enableAjaxValidation' => true,
24-
'enableClientValidation' => false,
25-
'validateOnChange' => false,
26-
'validateOnSubmit' => true,
27-
'validateOnBlur' => false,
28-
]);?>
24+
<?php
25+
26+
if ($enableActiveForm) {
27+
$form = ActiveForm::begin([
28+
'enableAjaxValidation' => true,
29+
'enableClientValidation' => false,
30+
'validateOnChange' => false,
31+
'validateOnSubmit' => true,
32+
'validateOnBlur' => false,
33+
]);
34+
} else {
35+
echo Html::beginForm();
36+
}
37+
38+
?>
2939

3040
<?php
3141

@@ -59,4 +69,10 @@
5969
?>
6070

6171
<?= Html::submitButton('Update', ['class' => 'btn btn-success']);?>
62-
<?php ActiveForm::end();?>
72+
<?php
73+
if ($enableActiveForm) {
74+
ActiveForm::end();
75+
} else {
76+
echo Html::endForm();
77+
}
78+
?>

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

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
validateOnType: false
7070
};
7171

72+
var isActiveFormEnabled = false;
73+
7274
var methods = {
7375
init: function (options) {
7476
var settings = $.extend(true, {}, defaultOptions, options || {}),
@@ -93,20 +95,26 @@
9395
addInput($(this));
9496
});
9597

98+
var i = 0,
99+
event = $.Event(events.afterInit);
100+
96101
var intervalID = setInterval(function () {
97102
if (typeof form.data('yiiActiveForm') === 'object') {
98-
var attribute = form.yiiActiveForm('find', id);
99-
var attributeDefaults = [];
103+
var attribute = form.yiiActiveForm('find', id),
104+
attributeDefaults = [];
105+
100106
if (typeof attribute === 'object') {
101107
$.each(attribute, function (key, value) {
102108
if (['id', 'input', 'container'].indexOf(key) == -1) {
103109
attributeDefaults[key] = value;
104110
}
105111
});
112+
106113
form.yiiActiveForm('remove', id);
107114
}
108115

109116
var attributeOptions = $.extend({}, defaultAttributeOptions, settings.attributeOptions);
117+
110118
$.each(attributeOptions, function (key, value) {
111119
if (typeof attributeDefaults[key] === 'undefined') {
112120
attributeDefaults[key] = value;
@@ -119,29 +127,43 @@
119127
addAttribute($(this));
120128
});
121129

122-
$wrapper.data('multipleInput').currentIndex = $wrapper
123-
.find('.multiple-input-list__item.' + settings.uniqueHash)
124-
.length;
130+
$wrapper.data('multipleInput').currentIndex = getCurrentIndex($wrapper);
131+
isActiveFormEnabled = true;
125132

126133
clearInterval(intervalID);
134+
$wrapper.trigger(event);
135+
} else {
136+
i++;
137+
}
138+
139+
// wait for initialization of ActiveForm a second
140+
// If after a second system could not detect ActiveForm it means
141+
// that widget is used without ActiveForm and we should just complete initialization of the widget
142+
if (i > 10) {
143+
$wrapper.data('multipleInput').currentIndex = getCurrentIndex($wrapper);
144+
isActiveFormEnabled = false;
127145

128-
var event = $.Event(events.afterInit);
146+
clearInterval(intervalID);
129147
$wrapper.trigger(event);
130148
}
131149
}, 100);
132150
},
151+
133152
add: function (values) {
134153
addInput($(this), values);
135154
},
155+
136156
remove: function (index) {
137157
var row = null;
138158
if (index) {
139159
row = $(this).find('.js-input-remove:eq(' + index + ')');
140160
} else {
141161
row = $(this).find('.js-input-remove').last();
142162
}
163+
143164
removeInput(row);
144165
},
166+
145167
clear: function () {
146168
$('.js-input-remove').each(function () {
147169
removeInput($(this));
@@ -211,7 +233,9 @@
211233
}
212234
}
213235

214-
addAttribute(that);
236+
if (isActiveFormEnabled) {
237+
addAttribute(that);
238+
}
215239

216240
index++;
217241
});
@@ -232,13 +256,16 @@
232256
if (count > settings.min) {
233257
var event = $.Event(events.beforeDeleteRow);
234258
$wrapper.trigger(event, [$toDelete]);
259+
235260
if (event.result === false) {
236261
return;
237262
}
238263

239-
$toDelete.find('input, select, textarea').each(function () {
240-
removeAttribute($(this));
241-
});
264+
if (isActiveFormEnabled) {
265+
$toDelete.find('input, select, textarea').each(function () {
266+
removeAttribute($(this));
267+
});
268+
}
242269

243270
$toDelete.fadeOut(300, function () {
244271
$(this).remove();
@@ -249,6 +276,11 @@
249276
}
250277
};
251278

279+
/**
280+
* Add an attribute to ActiveForm.
281+
*
282+
* @param input
283+
*/
252284
var addAttribute = function (input) {
253285
var id = getInputId(input);
254286

@@ -281,6 +313,9 @@
281313
}));
282314
};
283315

316+
/**
317+
* Removes an attribute from ActiveForm.
318+
*/
284319
var removeAttribute = function () {
285320
var id = getInputId($(this));
286321

@@ -309,7 +344,17 @@
309344
return id;
310345
};
311346

347+
var getCurrentIndex = function($wrapper) {
348+
if (typeof $wrapper.data('multipleInput') !== 'object') {
349+
return 0;
350+
}
351+
352+
return $wrapper
353+
.find('.multiple-input-list__item.' + $wrapper.data('multipleInput').settings.uniqueHash)
354+
.length;
355+
};
356+
312357
String.prototype.replaceAll = function (search, replace) {
313358
return this.split(search).join(replace);
314359
};
315-
})(window.jQuery);
360+
})(window.jQuery);

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.

0 commit comments

Comments
 (0)