Skip to content

Commit a8700f3

Browse files
committed
Fixed ajax validation for embedded fields
1 parent 53b7381 commit a8700f3

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

examples/views/embedded-input.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
/* @var $model ExampleModel */
1212

1313
$commonAttributeOptions = [
14-
'enableAjaxValidation' => false,
14+
'enableAjaxValidation' => true,
1515
'enableClientValidation' => false,
1616
'validateOnChange' => false,
1717
'validateOnSubmit' => true,
1818
'validateOnBlur' => false,
1919
];
2020

21-
$enableActiveForm = false;
21+
$enableActiveForm = true;
2222
?>
2323

2424
<?php

src/MultipleInput.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class MultipleInput extends InputWidget
113113

114114
/**
115115
* @var bool whether the widget is embedded or not.
116-
* @internal this property is used for internal purposes. Do not use it in your code.
116+
* @internal this property is used for internal purposes. Do not use it in your code.
117117
*/
118118
public $isEmbedded;
119119

@@ -190,7 +190,7 @@ protected function guessColumns()
190190
public function run()
191191
{
192192
$content = '';
193-
if ($this->hasModel()) {
193+
if ($this->hasModel() && $this->isEmbedded === false) {
194194
$content .= Html::hiddenInput(Html::getInputName($this->model, $this->attribute), null, [
195195
'id' => Html::getInputId($this->model, $this->attribute)
196196
]);

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,25 @@
341341
return;
342342
}
343343

344-
var data = wrapper.data('multipleInput');
345-
var bareID = id.replace(/-\d/, '').replace(/-\d-/, '');
344+
var data = wrapper.data('multipleInput'),
345+
attributeOptions = {};
346+
347+
// try to find options for embedded attribute at first.
348+
// For example the id of new input is example-1-field-0.
349+
// We remove last index and check whether attribute with such id exists or not.
350+
var bareId = id.replace(/-\d-([^\d]+)$/, '-$1');
351+
if (data.settings.attributes.hasOwnProperty(bareId)) {
352+
attributeOptions = data.settings.attributes[bareId];
353+
} else {
354+
// fallback in case of using flatten widget - just remove all digital indexes and check whether attribute
355+
// exists or not.
356+
bareId = bareId.replaceAll(/-\d-/, '-').replaceAll(/-\d/, '');
357+
if (data.settings.attributes.hasOwnProperty(bareId)) {
358+
attributeOptions = data.settings.attributes[bareId];
359+
}
360+
}
346361

347-
form.yiiActiveForm('add', $.extend({}, data.settings.attributes[bareID], {
362+
form.yiiActiveForm('add', $.extend({}, attributeOptions, {
348363
'id': id,
349364
'input': '#' + id,
350365
'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.

0 commit comments

Comments
 (0)