Skip to content

Commit

Permalink
use-associations-validations
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalRemis committed Apr 30, 2020
1 parent c7468a6 commit d548184
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
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

0 comments on commit d548184

Please sign in to comment.