From ec52c86f00ca07f675edc27914c837d4aa9dc888 Mon Sep 17 00:00:00 2001 From: "niraj.maharjan" Date: Wed, 11 Oct 2017 17:39:41 +0545 Subject: [PATCH 1/5] get error message function added --- src/simple-react-validator.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/simple-react-validator.js b/src/simple-react-validator.js index 75eb158..482c082 100644 --- a/src/simple-react-validator.js +++ b/src/simple-react-validator.js @@ -1,6 +1,7 @@ class SimpleReactValidator{ constructor(customRules = {}){ this.fields = []; + this.errorMessages = []; this.messagesShown = false; this.rules = { accepted : {message: 'The :attribute must be accepted.', rule: (val) => val === true }, @@ -79,6 +80,10 @@ class SimpleReactValidator{ } } + getErrorMessage(){ + return this.errorMessages; + } + // Private Methods _getRule(type){ return type.split(':')[0]; @@ -100,6 +105,7 @@ class SimpleReactValidator{ } _reactErrorElement(message, customClass){ + this.errorMessages.push(message); return React.createElement('div', {className: customClass || 'validation-message'}, message); } From 88871852b9cbf10ffda96dd43332d02a3e6f28cd Mon Sep 17 00:00:00 2001 From: "niraj.maharjan" Date: Wed, 11 Oct 2017 17:46:45 +0545 Subject: [PATCH 2/5] update --- src/simple-react-validator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/simple-react-validator.js b/src/simple-react-validator.js index 482c082..8a90e81 100644 --- a/src/simple-react-validator.js +++ b/src/simple-react-validator.js @@ -80,7 +80,7 @@ class SimpleReactValidator{ } } - getErrorMessage(){ + getErrorMessages(){ return this.errorMessages; } From bce4f213634ae9977977008ed1868f32062052bb Mon Sep 17 00:00:00 2001 From: "niraj.maharjan" Date: Wed, 11 Oct 2017 17:47:33 +0545 Subject: [PATCH 3/5] lint fix --- src/simple-react-validator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/simple-react-validator.js b/src/simple-react-validator.js index 8a90e81..86406df 100644 --- a/src/simple-react-validator.js +++ b/src/simple-react-validator.js @@ -105,7 +105,7 @@ class SimpleReactValidator{ } _reactErrorElement(message, customClass){ - this.errorMessages.push(message); + this.errorMessages.push(message); return React.createElement('div', {className: customClass || 'validation-message'}, message); } From e273c90f7c48b3cc9f84d2a5dddd8234d22220d5 Mon Sep 17 00:00:00 2001 From: "niraj.maharjan" Date: Thu, 12 Oct 2017 10:13:10 +0545 Subject: [PATCH 4/5] bug fix, error messages stored in field name key values --- src/simple-react-validator.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/simple-react-validator.js b/src/simple-react-validator.js index 86406df..6226d5f 100644 --- a/src/simple-react-validator.js +++ b/src/simple-react-validator.js @@ -28,6 +28,10 @@ class SimpleReactValidator{ }; } + getErrorMessages() { + return this.errorMessages; + } + showMessages(){ this.messagesShown = true; } @@ -54,6 +58,7 @@ class SimpleReactValidator{ } message(field, value, testString, customClass, customErrors = {}){ + this.errorMessages[field] = null; this.fields[field] = true; var tests = testString.split('|'); var rule, options, message; @@ -71,19 +76,14 @@ class SimpleReactValidator{ this.rules[rule].message.replace(':attribute', field.replace(/_/g, ' ')); if(options.length > 0 && this.rules[rule].hasOwnProperty('messageReplace')){ - return this._reactErrorElement(this.rules[rule].messageReplace(message, options)); + return this._reactErrorElement(this.rules[rule].messageReplace(message, options), false, field); } else { - return this._reactErrorElement(message, customClass); + return this._reactErrorElement(message, customClass, field); } } } } } - - getErrorMessages(){ - return this.errorMessages; - } - // Private Methods _getRule(type){ return type.split(':')[0]; @@ -104,8 +104,8 @@ class SimpleReactValidator{ arr.slice(-2).join(arr.length > 2 ? ', or ' : ' or '); } - _reactErrorElement(message, customClass){ - this.errorMessages.push(message); + _reactErrorElement(message, customClass, field){ + this.errorMessages[field] = message; return React.createElement('div', {className: customClass || 'validation-message'}, message); } From a71b81035742826e157374f77dc98cae94ac23ef Mon Sep 17 00:00:00 2001 From: Stuart Yamartino Date: Tue, 17 Oct 2017 13:12:15 -0400 Subject: [PATCH 5/5] simplify the assignment of the error messages --- src/simple-react-validator.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/simple-react-validator.js b/src/simple-react-validator.js index 6226d5f..2337fc6 100644 --- a/src/simple-react-validator.js +++ b/src/simple-react-validator.js @@ -1,7 +1,7 @@ class SimpleReactValidator{ constructor(customRules = {}){ - this.fields = []; - this.errorMessages = []; + this.fields = {}; + this.errorMessages = {}; this.messagesShown = false; this.rules = { accepted : {message: 'The :attribute must be accepted.', rule: (val) => val === true }, @@ -75,10 +75,11 @@ class SimpleReactValidator{ customErrors.default || this.rules[rule].message.replace(':attribute', field.replace(/_/g, ' ')); + this.errorMessages[field] = message; if(options.length > 0 && this.rules[rule].hasOwnProperty('messageReplace')){ - return this._reactErrorElement(this.rules[rule].messageReplace(message, options), false, field); + return this._reactErrorElement(this.rules[rule].messageReplace(message, options)); } else { - return this._reactErrorElement(message, customClass, field); + return this._reactErrorElement(message, customClass); } } } @@ -104,8 +105,7 @@ class SimpleReactValidator{ arr.slice(-2).join(arr.length > 2 ? ', or ' : ' or '); } - _reactErrorElement(message, customClass, field){ - this.errorMessages[field] = message; + _reactErrorElement(message, customClass){ return React.createElement('div', {className: customClass || 'validation-message'}, message); }