diff --git a/dist/client-side-validations.esm.js b/dist/client-side-validations.esm.js index bcd72be3f..2e399b96e 100644 --- a/dist/client-side-validations.esm.js +++ b/dist/client-side-validations.esm.js @@ -610,7 +610,8 @@ $.fn.isValid = function (validators) { if (obj.is('form')) { return validateForm(obj, validators); } else { - return validateElement(obj, validatorsFor(this[0].name, validators)); + var form = this.closest('form'); + return validateElement(obj, validatorsFor(this[0].name, validators, form)); } }; @@ -624,8 +625,13 @@ var cleanNestedElementName = function cleanNestedElementName(elementName, nested return elementName; }; -var cleanElementName = function cleanElementName(elementName, validators) { +var cleanElementName = function cleanElementName(elementName, validators, form) { elementName = elementName.replace(/\[(\w+_attributes)\]\[[\da-z_]+\](?=\[(?:\w+_attributes)\])/g, '[$1][]'); + + if (form.data('clientSideValidations').html_settings.type === 'SimpleForm::FormBuilder') { + elementName = elementName.replace(/\]\[\]$/g, ']'); // fix many association collections + } + var nestedMatches = elementName.match(/\[(\w+_attributes)\].*\[(\w+)\]$/); if (nestedMatches) { @@ -635,12 +641,12 @@ var cleanElementName = function cleanElementName(elementName, validators) { return elementName; }; -var validatorsFor = function validatorsFor(elementName, validators) { +var validatorsFor = function validatorsFor(elementName, validators, form) { if (Object.prototype.hasOwnProperty.call(validators, elementName)) { return validators[elementName]; } - return validators[cleanElementName(elementName, validators)] || {}; + return validators[cleanElementName(elementName, validators, form)] || {}; }; var validateForm = function validateForm(form, validators) { diff --git a/dist/client-side-validations.js b/dist/client-side-validations.js index 940740a47..a75eb4c09 100644 --- a/dist/client-side-validations.js +++ b/dist/client-side-validations.js @@ -616,7 +616,8 @@ if (obj.is('form')) { return validateForm(obj, validators); } else { - return validateElement(obj, validatorsFor(this[0].name, validators)); + var form = this.closest('form'); + return validateElement(obj, validatorsFor(this[0].name, validators, form)); } }; @@ -630,8 +631,13 @@ return elementName; }; - var cleanElementName = function cleanElementName(elementName, validators) { + var cleanElementName = function cleanElementName(elementName, validators, form) { elementName = elementName.replace(/\[(\w+_attributes)\]\[[\da-z_]+\](?=\[(?:\w+_attributes)\])/g, '[$1][]'); + + if (form.data('clientSideValidations').html_settings.type === 'SimpleForm::FormBuilder') { + elementName = elementName.replace(/\]\[\]$/g, ']'); // fix many association collections + } + var nestedMatches = elementName.match(/\[(\w+_attributes)\].*\[(\w+)\]$/); if (nestedMatches) { @@ -641,12 +647,12 @@ return elementName; }; - var validatorsFor = function validatorsFor(elementName, validators) { + var validatorsFor = function validatorsFor(elementName, validators, form) { if (Object.prototype.hasOwnProperty.call(validators, elementName)) { return validators[elementName]; } - return validators[cleanElementName(elementName, validators)] || {}; + return validators[cleanElementName(elementName, validators, form)] || {}; }; var validateForm = function validateForm(form, validators) { diff --git a/src/main.js b/src/main.js index 91f2553cc..c43ac13e7 100644 --- a/src/main.js +++ b/src/main.js @@ -66,7 +66,8 @@ $.fn.isValid = function (validators) { if (obj.is('form')) { return validateForm(obj, validators) } else { - return validateElement(obj, validatorsFor(this[0].name, validators)) + const form = this.closest('form') + return validateElement(obj, validatorsFor(this[0].name, validators, form)) } } @@ -80,9 +81,13 @@ const cleanNestedElementName = (elementName, nestedMatches, validators) => { return elementName } -const cleanElementName = (elementName, validators) => { +const cleanElementName = (elementName, validators, form) => { elementName = elementName.replace(/\[(\w+_attributes)\]\[[\da-z_]+\](?=\[(?:\w+_attributes)\])/g, '[$1][]') + if (form.data('clientSideValidations').html_settings.type === 'SimpleForm::FormBuilder') { + elementName = elementName.replace(/\]\[\]$/g, ']') // fix many association collections + } + const nestedMatches = elementName.match(/\[(\w+_attributes)\].*\[(\w+)\]$/) if (nestedMatches) { @@ -92,12 +97,12 @@ const cleanElementName = (elementName, validators) => { return elementName } -const validatorsFor = (elementName, validators) => { +const validatorsFor = (elementName, validators, form) => { if (Object.prototype.hasOwnProperty.call(validators, elementName)) { return validators[elementName] } - return validators[cleanElementName(elementName, validators)] || {} + return validators[cleanElementName(elementName, validators, form)] || {} } const validateForm = (form, validators) => { diff --git a/vendor/assets/javascripts/rails.validations.js b/vendor/assets/javascripts/rails.validations.js index 940740a47..a75eb4c09 100644 --- a/vendor/assets/javascripts/rails.validations.js +++ b/vendor/assets/javascripts/rails.validations.js @@ -616,7 +616,8 @@ if (obj.is('form')) { return validateForm(obj, validators); } else { - return validateElement(obj, validatorsFor(this[0].name, validators)); + var form = this.closest('form'); + return validateElement(obj, validatorsFor(this[0].name, validators, form)); } }; @@ -630,8 +631,13 @@ return elementName; }; - var cleanElementName = function cleanElementName(elementName, validators) { + var cleanElementName = function cleanElementName(elementName, validators, form) { elementName = elementName.replace(/\[(\w+_attributes)\]\[[\da-z_]+\](?=\[(?:\w+_attributes)\])/g, '[$1][]'); + + if (form.data('clientSideValidations').html_settings.type === 'SimpleForm::FormBuilder') { + elementName = elementName.replace(/\]\[\]$/g, ']'); // fix many association collections + } + var nestedMatches = elementName.match(/\[(\w+_attributes)\].*\[(\w+)\]$/); if (nestedMatches) { @@ -641,12 +647,12 @@ return elementName; }; - var validatorsFor = function validatorsFor(elementName, validators) { + var validatorsFor = function validatorsFor(elementName, validators, form) { if (Object.prototype.hasOwnProperty.call(validators, elementName)) { return validators[elementName]; } - return validators[cleanElementName(elementName, validators)] || {}; + return validators[cleanElementName(elementName, validators, form)] || {}; }; var validateForm = function validateForm(form, validators) {