Skip to content

Commit 8b835af

Browse files
ENGCOM-5174: issue fixed #8258 - assign indices for all array inputs so that validation works properly #15383
- Merge Pull Request #15383 from jayankaghosh/magento2:issue-8258 - Merged commits: 1. 113440f 2. 874d770 3. 84355a3 4. d5c41b6 5. 156ac16 6. 65a2c17 7. 97c03f5 8. 224f737 9. 908100b 10. 38b9709 11. 388d31d 12. ee288c1
2 parents 1656409 + ee288c1 commit 8b835af

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

lib/web/mage/validation.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,6 +1906,7 @@
19061906
* @protected
19071907
*/
19081908
_create: function () {
1909+
this._prepareArrayInputs();
19091910
this.validate = this.element.validate(this.options);
19101911

19111912
// ARIA (adding aria-required attribute)
@@ -1918,6 +1919,49 @@
19181919
this._listenFormValidate();
19191920
},
19201921

1922+
/**
1923+
* Validation creation.
1924+
*
1925+
* @protected
1926+
*/
1927+
_prepareArrayInputs: function () {
1928+
/* Store original names for array inputs */
1929+
var originalElements = [],
1930+
originalSubmitHandler = this.options.submitHandler;
1931+
1932+
/* For all array inputs, assign index so that validation is proper */
1933+
this.element.find('[name$="[]"]').each(function (key, input) {
1934+
var originalName, name;
1935+
1936+
input = $(input);
1937+
originalName = input.attr('name');
1938+
name = originalName.replace('[]', '[' + key + ']');
1939+
$(input).attr('name', name);
1940+
$(input).attr('orig-name', originalName);
1941+
originalElements.push({
1942+
element: $(input),
1943+
name: originalName
1944+
});
1945+
});
1946+
1947+
if (originalElements.length) {
1948+
/**
1949+
* Before submitting the actual form, remove the previously assigned indices
1950+
* @param {Object} form
1951+
*/
1952+
this.options.submitHandler = function (form, event) {
1953+
originalElements.forEach(function (element) {
1954+
element.element.attr('name', element.name);
1955+
element.element.removeAttr('orig-name');
1956+
});
1957+
1958+
console.error(this.submit)
1959+
/* Call the originalSubmitHandler if it's a function */
1960+
typeof originalSubmitHandler === 'function' ? originalSubmitHandler(form) : form.submit();
1961+
};
1962+
}
1963+
},
1964+
19211965
/**
19221966
* Validation listening.
19231967
* @protected

lib/web/mage/validation/validation.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,13 @@
4949
'validate-one-checkbox-required-by-name': [
5050
function (value, element, params) {
5151
var checkedCount = 0,
52+
selector,
5253
container;
5354

5455
if (element.type === 'checkbox') {
55-
$('[name="' + element.name + '"]').each(function () {
56+
/* If orig-name attribute is present, use it for validation. Else use name */
57+
selector = element.getAttribute('orig-name') ? '[orig-name="' + element.getAttribute('orig-name') + '"]' : '[name="' + element.name + '"]';
58+
$(selector).each(function () {
5659
if ($(this).is(':checked')) {
5760
checkedCount += 1;
5861

0 commit comments

Comments
 (0)