Simplified validate function. Removed validateAll function, merged into validate function. Cleaned up code.
This commit is contained in:
parent
368f713dba
commit
bf7bd34567
@ -99,7 +99,7 @@ function notify(target, type, msg, duration) {
|
||||
* @param {Number} duration The number of miliseconds to show the error. Default is 4000. -1 is infinite.
|
||||
* @returns {HTMLElement} The created notification HTMLElement.
|
||||
*/
|
||||
HTMLElement.prototype.error = function(msg, duration = 4000) {
|
||||
HTMLElement.prototype.error = function (msg, duration = 4000) {
|
||||
return notify(this, "error", msg, duration);
|
||||
}
|
||||
|
||||
@ -109,7 +109,7 @@ function notify(target, type, msg, duration) {
|
||||
* @param {Number} duration The number of miliseconds to show the warning. Default is 4000. -1 is infinite.
|
||||
* @returns {HTMLElement} The created notification HTMLElement.
|
||||
*/
|
||||
HTMLElement.prototype.warning = function(msg, duration = 4000) {
|
||||
HTMLElement.prototype.warning = function (msg, duration = 4000) {
|
||||
return notify(this, "warning", msg, duration);
|
||||
}
|
||||
|
||||
@ -119,7 +119,7 @@ function notify(target, type, msg, duration) {
|
||||
* @param {Number} duration The number of miliseconds to show the notification. Default is 4000. -1 is infinite.
|
||||
* @returns {HTMLElement} The created notification HTMLElement.
|
||||
*/
|
||||
HTMLElement.prototype.success = function(msg, duration = 4000) {
|
||||
HTMLElement.prototype.success = function (msg, duration = 4000) {
|
||||
return notify(this, "success", msg, duration);
|
||||
}
|
||||
|
||||
@ -129,7 +129,7 @@ function notify(target, type, msg, duration) {
|
||||
* @param {Number} duration The number of miliseconds to show the notification. Default is 4000. -1 is infinite.
|
||||
* @returns {HTMLElement} The created notification HTMLElement.
|
||||
*/
|
||||
HTMLElement.prototype.info = function(msg, duration = 4000) {
|
||||
HTMLElement.prototype.info = function (msg, duration = 4000) {
|
||||
notify(this, "info", msg, duration);
|
||||
}
|
||||
|
||||
@ -144,7 +144,7 @@ function notify(target, type, msg, duration) {
|
||||
* })
|
||||
* @returns {IgniteTemplate} This ignite template.
|
||||
*/
|
||||
IgniteTemplate.prototype.validate = function(callback) {
|
||||
IgniteTemplate.prototype.validate = function (callback) {
|
||||
//Setup the validators array if it doesn't exist.
|
||||
if (!this._validators) {
|
||||
this._validators = [];
|
||||
@ -157,16 +157,6 @@ function notify(target, type, msg, duration) {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
//Setup a constructor to create a validate function on the created element.
|
||||
this._constructors.push(() => this.element.validate = () => {
|
||||
for (var i = 0; i < this._validators.length; i++) {
|
||||
if (!this._validators[i]()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
//Register a new validator
|
||||
@ -204,9 +194,11 @@ function notify(target, type, msg, duration) {
|
||||
* @param {string|Function|IgniteProperty} msg The message to display when the email is incorrect. If null a default one will show.
|
||||
* @returns {IgniteTemplate} This ignite template.
|
||||
*/
|
||||
IgniteTemplate.prototype.validateEmail = function(msg) {
|
||||
IgniteTemplate.prototype.validateEmail = function (msg) {
|
||||
return this.validate((value, error) => {
|
||||
if (!value || value.trim().length < 5) {
|
||||
if (!value || value.length == 0) {
|
||||
return error(msg ? msg : `Please enter an email address.`);
|
||||
} else if (value.trim().length < 5) {
|
||||
return error(msg ? msg : `Email address too short.`);
|
||||
} else if (!value.includes('@')) {
|
||||
return error(msg ? msg : 'Email address missing @ symbol.');
|
||||
@ -225,7 +217,7 @@ function notify(target, type, msg, duration) {
|
||||
* @param {string|Function|IgniteProperty} msg The message to display when the email is incorrect. If null a default one will show.
|
||||
* @returns {IgniteTemplate} This ignite template.
|
||||
*/
|
||||
IgniteTemplate.prototype.validatePassword = function(msg) {
|
||||
IgniteTemplate.prototype.validatePassword = function (msg) {
|
||||
return this.validate((value, error) => {
|
||||
if (!value || value.trim().length < 5) {
|
||||
return error(msg ? msg : `Password is must be at least 5 characters.`);
|
||||
@ -243,7 +235,7 @@ function notify(target, type, msg, duration) {
|
||||
* @param {string|Function|IgniteProperty} msg The message to display when the input length is too short. If null a default one will show.
|
||||
* @returns {IgniteTemplate} This ignite template.
|
||||
*/
|
||||
IgniteTemplate.prototype.validateMin = function(min, msg) {
|
||||
IgniteTemplate.prototype.validateMin = function (min, msg) {
|
||||
return this.validate((value, error) => {
|
||||
if (!value || value.toString().trim().length < min) {
|
||||
return error(msg ? msg : `Input must contain at least ${min} character(s).`);
|
||||
@ -257,7 +249,7 @@ function notify(target, type, msg, duration) {
|
||||
* @param {string|Function|IgniteProperty} msg The message to display when the input length is too long. If null a default one will show.
|
||||
* @returns {IgniteTemplate} This ignite template.
|
||||
*/
|
||||
IgniteTemplate.prototype.validateMax = function(max, msg) {
|
||||
IgniteTemplate.prototype.validateMax = function (max, msg) {
|
||||
return this.validate((value, error) => {
|
||||
if (!value || value.toString().trim().length > max) {
|
||||
return error(msg ? msg : `Input must contain less than ${max} character(s).`);
|
||||
@ -271,7 +263,7 @@ function notify(target, type, msg, duration) {
|
||||
* @param {string|Function|IgniteProperty} msg The message to display when the input length is too long. If null a default one will show.
|
||||
* @returns {IgniteTemplate} This ignite template.
|
||||
*/
|
||||
IgniteTemplate.prototype.validateNot = function(expected, msg) {
|
||||
IgniteTemplate.prototype.validateNot = function (expected, msg) {
|
||||
return this.validate((value, error) => {
|
||||
if (!value || value == expected) {
|
||||
return error(msg ? msg : `Input cannot be ${expected}.`);
|
||||
@ -285,7 +277,7 @@ function notify(target, type, msg, duration) {
|
||||
* @param {string|Function|IgniteProperty} msg The message to display when the input doesn't contain the string. If null a default one will show.
|
||||
* @returns {IgniteTemplate} This ignite template.
|
||||
*/
|
||||
IgniteTemplate.prototype.validateIncludes = function(str, msg) {
|
||||
IgniteTemplate.prototype.validateIncludes = function (str, msg) {
|
||||
return this.validate((value, error) => {
|
||||
if (!value || !value.toString().includes(str)) {
|
||||
return error(msg ? msg : `Input must contain ${str}.`);
|
||||
@ -299,7 +291,7 @@ function notify(target, type, msg, duration) {
|
||||
* @param {string|Function|IgniteProperty} msg The message to display when the input doesn't match. If null a default one will show.
|
||||
* @returns {IgniteTemplate} This ignite template.
|
||||
*/
|
||||
IgniteTemplate.prototype.validateMatches = function(compare, msg) {
|
||||
IgniteTemplate.prototype.validateMatches = function (compare, msg) {
|
||||
return this.validate((value, error) => {
|
||||
if (compare instanceof Function) {
|
||||
if (compare() != value) {
|
||||
@ -316,15 +308,33 @@ function notify(target, type, msg, duration) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates all the children of an element and returns if they are valid or not.
|
||||
* @returns {Boolean} Whether or not all the children of this element are valid.
|
||||
* Validates this element and all the children within the element.
|
||||
* @returns {Boolean} Whether or not this element and all children in this element are valid.
|
||||
*/
|
||||
HTMLElement.prototype.validateAll = function() {
|
||||
var elements = this.getElementsByTagName("*");
|
||||
for (var i = 0; i < elements.length; i++) {
|
||||
if (elements[i].validate != undefined && !elements[i].validate()) {
|
||||
return false;
|
||||
HTMLElement.prototype.validate = function () {
|
||||
//If this element has a template with validators run them.
|
||||
if (this.template && this.template._validators) {
|
||||
for (var i = 0; i < this.template._validators.length; i++) {
|
||||
if (!this.template._validators[i]()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//Look for elements that we can try to validate.
|
||||
var elements = this.getElementsByTagName("*");
|
||||
for (var i = 0; i < elements.length; i++) {
|
||||
var element = elements[i];
|
||||
|
||||
//If this element has a template, and has validators, run them, if any are false, return false.
|
||||
if (element.template && element.template._validators) {
|
||||
for (var v = 0; v < element.template._validators.length; v++) {
|
||||
if (!element.template._validators[v]()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user