Skip to content

Commit

Permalink
Merge pull request #4 from neroze/feature/get_error_messages
Browse files Browse the repository at this point in the history
method to get error messages
  • Loading branch information
stuyam authored Oct 24, 2017
2 parents bbbfe11 + a71b810 commit d32d172
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/simple-react-validator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class SimpleReactValidator{
constructor(customRules = {}){
this.fields = [];
this.fields = {};
this.errorMessages = {};
this.messagesShown = false;
this.rules = {
accepted : {message: 'The :attribute must be accepted.', rule: (val) => val === true },
Expand All @@ -27,6 +28,10 @@ class SimpleReactValidator{
};
}

getErrorMessages() {
return this.errorMessages;
}

showMessages(){
this.messagesShown = true;
}
Expand Down Expand Up @@ -58,6 +63,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;
Expand All @@ -74,6 +80,7 @@ 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));
} else {
Expand All @@ -83,7 +90,6 @@ class SimpleReactValidator{
}
}
}

// Private Methods
_getRule(type){
return type.split(':')[0];
Expand Down

0 comments on commit d32d172

Please sign in to comment.