Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use-associations-validations #787

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions dist/client-side-validations.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};

Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
14 changes: 10 additions & 4 deletions dist/client-side-validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};

Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
13 changes: 9 additions & 4 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand All @@ -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) {
Expand All @@ -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) => {
Expand Down
14 changes: 10 additions & 4 deletions vendor/assets/javascripts/rails.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};

Expand All @@ -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) {
Expand All @@ -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) {
Expand Down