Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
validate-mixin\
import $ from 'jquery';
import React from 'react';
let capitalizeFirstLetter = (string) => {
return string.charAt(0).toUpperCase() + string.slice(1);
};
let validateHelper = {
required(value, name) {
if (value) {
return false;
} else {
return capitalizeFirstLetter(name) + " field is required";
}
},
num(value, name, count) {
if (isNaN(parseInt(value))) {
return `${capitalizeFirstLetter(name)} must be a number`;
} else {
return false;
}
},
min(value, name, count) {
if (value && value.length >= parseInt(count)) {
return false;
} else {
return `${capitalizeFirstLetter(name)} must be at least ${count} characters`;
}
},
minInt(value, name, count) {
if (typeof value === 'string') {
value = parseInt(value);
}
if (value && value>= parseInt(count)) {
return false;
} else {
return `${capitalizeFirstLetter(name)} must be at least ${count}`;
}
},
max(value, name, count) {
if (value && value.length <= parseInt(count)) {
return false;
} else {
return `${capitalizeFirstLetter(name)} must be less than or equal to ${count}`;
}
},
maxInt(value, name, count) {
if (typeof value === 'string') {
value = parseInt(value);
}
if (value <= parseInt(count)) {
return false;
} else {
return `${capitalizeFirstLetter(name)} must be less than or equal to ${count}`;
}
},
email(value, name) {
let re = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
if (re.test(value)) {
return false;
} else {
return capitalizeFirstLetter(name) + " must be a valid";
}
},
checkFloat(value, name) {
value = value.replace(/\,/g, '.');
if (value && !isNaN(parseFloat(value))) {
return false;
} else {
return capitalizeFirstLetter(name) + " must be a valid";
}
},
// Manipulators
setInt(value) {
return (parseInt(value));
},
setFloat(value) {
value = value.replace(/\,/g, '.');
return (parseFloat(value));
},
select2(value, field) {
return $(field).select2('data');
}
};
export default {
validate(fields=this.state.fields, action='all', clearErrors=false) {
let errors = this.state.errors;
if (clearErrors) {
errors = {};
}
for (let [key, value] of Object.entries(fields)) {
let field = React.findDOMNode(this.refs[key]);
if (!field) {
continue;
}
let fieldValue = field.value;
for (let validateType of value) {
if (typeof validateType === 'function') {
let validateResult = validateType.apply(null, [fieldValue, key]);
if (validateResult) {
errors[key] = validateResult;
break;
} else {
delete errors[key];
}
break;
}
let validateSplit = validateType.split(':');
let validateAction = validateSplit[validateSplit.length-1];
if (validateAction === 'all' || validateAction === action) {
let helperArg = null;
if (validateSplit.length > 2) {
helperArg = validateSplit[1];
}
let validateResult = validateHelper[validateSplit[0]](fieldValue, key, helperArg);
if (validateResult) {
errors[key] = validateResult;
break;
} else {
delete errors[key];
}
}
}
}
return {errors: errors};
},
getData(fields=this.state.fields, manipulators=[]) {
let data = {};
for (let [key, value] of Object.entries(fields)) {
let field = React.findDOMNode(this.refs[key]);
if (!field) {
continue;
}
let fieldValue = field.value;
if (field.type === 'checkbox') {
fieldValue = field.checked;
} else if (field.type === 'radio') {
for (let radio of field.form.querySelectorAll(`input[name=${field.name}]`)) {
if (radio.checked) {
fieldValue = radio.value;
break;
}
}
}
for (let man of manipulators) {
let manSplit = man.split(':');
if (manSplit[0] === key) {
fieldValue = validateHelper[manSplit[1]].apply(null, [fieldValue, field]);
}
}
data[key] = fieldValue;
}
return data;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment