- {/* This is where the magic happens */}
+
+ {/********** This is where the magic happens ***********/}
{this.validator.message('title', this.state.title, 'required|alpha')}
+
- {/* This is where the magic happens */}
+
+ {/********** This is where the magic happens ***********/}
{this.validator.message('email', this.state.email, 'required|email', 'text-danger')}
+
- {/* This is where the magic happens */}
+
+ {/********** This is where the magic happens ***********/}
{this.validator.message('review', this.state.review, 'required|min:20|max:120', false, {min: 'Custom min error'})}
+
diff --git a/bower.json b/bower.json
index b7dcec7..9b1d818 100644
--- a/bower.json
+++ b/bower.json
@@ -2,7 +2,7 @@
"name": "simple-react-validator",
"description": "A simple react form validator inspired by Laravel validation.",
"main": "dist/simple-react-validator.min.js",
- "version": "0.0.4",
+ "version": "0.0.6",
"authors": [
"Stuart Yamartino"
],
diff --git a/dist/simple-react-validator.js b/dist/simple-react-validator.js
index 1803193..5d42e92 100644
--- a/dist/simple-react-validator.js
+++ b/dist/simple-react-validator.js
@@ -24,7 +24,8 @@ var SimpleReactValidator = function () {
_classCallCheck(this, SimpleReactValidator);
- this.fields = [];
+ this.fields = {};
+ this.errorMessages = {};
this.messagesShown = false;
this.rules = _extends({
accepted: { message: 'The :attribute must be accepted.', rule: function rule(val) {
@@ -107,6 +108,11 @@ var SimpleReactValidator = function () {
}
_createClass(SimpleReactValidator, [{
+ key: 'getErrorMessages',
+ value: function getErrorMessages() {
+ return this.errorMessages;
+ }
+ }, {
key: 'showMessages',
value: function showMessages() {
this.messagesShown = true;
@@ -152,6 +158,7 @@ var SimpleReactValidator = function () {
value: function message(field, value, testString, customClass) {
var customErrors = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
+ this.errorMessages[field] = null;
this.fields[field] = true;
var tests = testString.split('|');
var rule, options, message;
@@ -166,6 +173,7 @@ var SimpleReactValidator = function () {
if (this.messagesShown) {
message = customErrors[rule] || 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 {
@@ -175,7 +183,6 @@ var SimpleReactValidator = function () {
}
}
}
-
// Private Methods
}, {
diff --git a/dist/simple-react-validator.min.js b/dist/simple-react-validator.min.js
index e4bdb5b..9769969 100644
--- a/dist/simple-react-validator.min.js
+++ b/dist/simple-react-validator.min.js
@@ -1,2 +1,2 @@
// Simple React Validator v0.0.5 | Created By Dockwa | MIT License | 2017
-!function(e,t){"function"==typeof define&&define.amd?define(["react"],t):"object"==typeof exports?module.exports=t(require("react")):e.SimpleReactValidator=t(e.React)}(this,function(e){"use strict";function t(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var r=Object.assign||function(e){for(var t=1;t0&&void 0!==arguments[0]?arguments[0]:{};t(this,s),this.fields=[],this.messagesShown=!1,this.rules=r({accepted:{message:"The :attribute must be accepted.",rule:function(e){return!0===e}},alpha:{message:"The :attribute may only contain letters.",rule:function(t){return e._testRegex(t,/^[A-Z]*$/i)}},alpha_num:{message:"The :attribute may only contain letters and numbers.",rule:function(t){return e._testRegex(t,/^[A-Z0-9]*$/i)}},alpha_num_dash:{message:"The :attribute may only contain letters, numbers, and dashes.",rule:function(t){return e._testRegex(t,/^[A-Z0-9_-]*$/i)}},card_exp:{message:"The :attribute must be a valid expiration date.",rule:function(t){return e._testRegex(t,/^(([0]?[1-9]{1})|([1]{1}[0-2]{1}))\s?\/\s?(\d{2}|\d{4})$/)}},card_num:{message:"The :attribute must be a valid credit card number.",rule:function(t){return e._testRegex(t,/^\d{4}\s?\d{4,6}\s?\d{4,5}\s?\d{0,8}$/)}},email:{message:"The :attribute must be a valid email address.",rule:function(t){return e._testRegex(t,/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i)}},gt:{message:"The :attribute must be greater than :gt.",rule:function(t,r){return!!e._testRegex(t,/^\d+.?\d*$/)&&parseFloat(t)>parseFloat(r[0])},messageReplace:function(e,t){return e.replace(":gt",t[0])}},gte:{message:"The :attribute must be greater than or equal to :gte.",rule:function(t,r){return!!e._testRegex(t,/^\d+.?\d*$/)&&parseFloat(t)>=parseFloat(r[0])},messageReplace:function(e,t){return e.replace(":gte",t[0])}},in:{message:"The selected :attribute must be :values.",rule:function(e,t){return t.indexOf(e)>-1},messageReplace:function(t,r){return t.replace(":values",e._toSentence(r))}},integer:{message:"The :attribute must be an integer.",rule:function(t){return e._testRegex(t,/^\d+$/)}},lt:{message:"The :attribute must be less than :lt.",rule:function(t,r){return!!e._testRegex(t,/^\d+.?\d*$/)&&parseFloat(t)=t[0]},messageReplace:function(e,t){return e.replace(":min",t[0])}},not_in:{message:"The selected :attribute must not be :values.",rule:function(e,t){return-1===t.indexOf(e)},messageReplace:function(t,r){return t.replace(":values",e._toSentence(r))}},numeric:{message:"The :attribute must be a number.",rule:function(t){return e._testRegex(t,/^\d+.?\d*$/)}},phone:{message:"The :attribute must be a valid phone number.",rule:function(t){return e._testRegex(t,/(\+?\d{0,4})?\s?-?\s?(\(?\d{3}\)?)\s?-?\s?(\(?\d{3}\)?)\s?-?\s?(\(?\d{4}\)?)/)}},required:{message:"The :attribute field is required.",rule:function(t){return e._testRegex(t,/.+/)}},url:{message:"The :attribute must be a url.",rule:function(t){return e._testRegex(t,/^(https?|ftp):\/\/(-\.)?([^\s\/?\.#-]+\.?)+(\/[^\s]*)?$/i)}}},n)}return n(s,[{key:"showMessages",value:function(){this.messagesShown=!0}},{key:"hideMessages",value:function(){this.messagesShown=!1}},{key:"allValid",value:function(){for(var e in this.fields)if(!1===this.fieldValid(e))return!1;return!0}},{key:"fieldValid",value:function(e){return this.fields.hasOwnProperty(e)&&!0===this.fields[e]}},{key:"customMessage",value:function(e,t){if(e&&this.messagesShown)return this._reactErrorElement(e,t)}},{key:"message",value:function(e,t,r,n){var s=arguments.length>4&&void 0!==arguments[4]?arguments[4]:{};this.fields[e]=!0;for(var a,u,i,l=r.split("|"),o=0;o0&&this.rules[a].hasOwnProperty("messageReplace")?this._reactErrorElement(this.rules[a].messageReplace(i,u)):this._reactErrorElement(i,n)}},{key:"_getRule",value:function(e){return e.split(":")[0]}},{key:"_getOptions",value:function(e){var t=e.split(":");return t.length>1?t[1].split(","):[]}},{key:"_valueOrEmptyString",value:function(e){return void 0===e||null===e?"":e}},{key:"_toSentence",value:function(e){return e.slice(0,-2).join(", ")+(e.slice(0,-2).length?", ":"")+e.slice(-2).join(e.length>2?", or ":" or ")}},{key:"_reactErrorElement",value:function(t,r){return e.createElement("div",{className:r||"validation-message"},t)}},{key:"_testRegex",value:function(e,t){return null!==e.toString().match(t)}}]),s}()});
\ No newline at end of file
+!function(e,t){"function"==typeof define&&define.amd?define(["react"],t):"object"==typeof exports?module.exports=t(require("react")):e.SimpleReactValidator=t(e.React)}(this,function(e){"use strict";function t(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var r=Object.assign||function(e){for(var t=1;t0&&void 0!==arguments[0]?arguments[0]:{};t(this,n),this.fields={},this.errorMessages={},this.messagesShown=!1,this.rules=r({accepted:{message:"The :attribute must be accepted.",rule:function(e){return!0===e}},alpha:{message:"The :attribute may only contain letters.",rule:function(t){return e._testRegex(t,/^[A-Z]*$/i)}},alpha_num:{message:"The :attribute may only contain letters and numbers.",rule:function(t){return e._testRegex(t,/^[A-Z0-9]*$/i)}},alpha_num_dash:{message:"The :attribute may only contain letters, numbers, and dashes.",rule:function(t){return e._testRegex(t,/^[A-Z0-9_-]*$/i)}},card_exp:{message:"The :attribute must be a valid expiration date.",rule:function(t){return e._testRegex(t,/^(([0]?[1-9]{1})|([1]{1}[0-2]{1}))\s?\/\s?(\d{2}|\d{4})$/)}},card_num:{message:"The :attribute must be a valid credit card number.",rule:function(t){return e._testRegex(t,/^\d{4}\s?\d{4,6}\s?\d{4,5}\s?\d{0,8}$/)}},email:{message:"The :attribute must be a valid email address.",rule:function(t){return e._testRegex(t,/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i)}},gt:{message:"The :attribute must be greater than :gt.",rule:function(t,r){return!!e._testRegex(t,/^\d+.?\d*$/)&&parseFloat(t)>parseFloat(r[0])},messageReplace:function(e,t){return e.replace(":gt",t[0])}},gte:{message:"The :attribute must be greater than or equal to :gte.",rule:function(t,r){return!!e._testRegex(t,/^\d+.?\d*$/)&&parseFloat(t)>=parseFloat(r[0])},messageReplace:function(e,t){return e.replace(":gte",t[0])}},in:{message:"The selected :attribute must be :values.",rule:function(e,t){return t.indexOf(e)>-1},messageReplace:function(t,r){return t.replace(":values",e._toSentence(r))}},integer:{message:"The :attribute must be an integer.",rule:function(t){return e._testRegex(t,/^\d+$/)}},lt:{message:"The :attribute must be less than :lt.",rule:function(t,r){return!!e._testRegex(t,/^\d+.?\d*$/)&&parseFloat(t)=t[0]},messageReplace:function(e,t){return e.replace(":min",t[0])}},not_in:{message:"The selected :attribute must not be :values.",rule:function(e,t){return-1===t.indexOf(e)},messageReplace:function(t,r){return t.replace(":values",e._toSentence(r))}},numeric:{message:"The :attribute must be a number.",rule:function(t){return e._testRegex(t,/^\d+.?\d*$/)}},phone:{message:"The :attribute must be a valid phone number.",rule:function(t){return e._testRegex(t,/(\+?\d{0,4})?\s?-?\s?(\(?\d{3}\)?)\s?-?\s?(\(?\d{3}\)?)\s?-?\s?(\(?\d{4}\)?)/)}},required:{message:"The :attribute field is required.",rule:function(t){return e._testRegex(t,/.+/)}},url:{message:"The :attribute must be a url.",rule:function(t){return e._testRegex(t,/^(https?|ftp):\/\/(-\.)?([^\s\/?\.#-]+\.?)+(\/[^\s]*)?$/i)}}},s)}return s(n,[{key:"getErrorMessages",value:function(){return this.errorMessages}},{key:"showMessages",value:function(){this.messagesShown=!0}},{key:"hideMessages",value:function(){this.messagesShown=!1}},{key:"allValid",value:function(){for(var e in this.fields)if(!1===this.fieldValid(e))return!1;return!0}},{key:"fieldValid",value:function(e){return this.fields.hasOwnProperty(e)&&!0===this.fields[e]}},{key:"customMessage",value:function(e,t){if(e&&this.messagesShown)return this._reactErrorElement(e,t)}},{key:"message",value:function(e,t,r,s){var n=arguments.length>4&&void 0!==arguments[4]?arguments[4]:{};this.errorMessages[e]=null,this.fields[e]=!0;for(var a,u,i,l=r.split("|"),o=0;o0&&this.rules[a].hasOwnProperty("messageReplace")?this._reactErrorElement(this.rules[a].messageReplace(i,u)):this._reactErrorElement(i,s)}},{key:"_getRule",value:function(e){return e.split(":")[0]}},{key:"_getOptions",value:function(e){var t=e.split(":");return t.length>1?t[1].split(","):[]}},{key:"_valueOrEmptyString",value:function(e){return void 0===e||null===e?"":e}},{key:"_toSentence",value:function(e){return e.slice(0,-2).join(", ")+(e.slice(0,-2).length?", ":"")+e.slice(-2).join(e.length>2?", or ":" or ")}},{key:"_reactErrorElement",value:function(t,r){return e.createElement("div",{className:r||"validation-message"},t)}},{key:"_testRegex",value:function(e,t){return null!==e.toString().match(t)}}]),n}()});
\ No newline at end of file
diff --git a/package.json b/package.json
index df66399..66ec6e1 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "simple-react-validator",
- "version": "0.0.5",
+ "version": "0.0.6",
"description": "A simple react form validator inspired by Laravel validation.",
"main": "dist/simple-react-validator.min.js",
"dependencies": {},