1 var Validate =
function(table){
6 Validate.prototype.initializeColumn =
function(column){
11 if(column.definition.validator){
13 if(Array.isArray(column.definition.validator)){
14 column.definition.validator.forEach(
function(item){
15 validator =
self._extractValidator(item);
18 config.push(validator);
23 validator = this._extractValidator(column.definition.validator);
26 config.push(validator);
30 column.modules.validate = config.length ? config :
false;
34 Validate.prototype._extractValidator =
function(value){
35 var type, params, pos;
39 pos = value.indexOf(
':');
42 type = value.substring(0,pos);
43 params = value.substring(pos+1);
48 return this._buildValidator(type, params);
52 return this._buildValidator(value);
56 return this._buildValidator(value.type, value.parameters);
61 Validate.prototype._buildValidator =
function(type, params){
63 var func = typeof type ==
"function" ? type : this.validators[type];
66 console.warn(
"Validator Setup Error - No matching validator found:", type);
70 type:typeof type ==
"function" ?
"function" : type,
78 Validate.prototype.validate =
function(validators, cell, value){
83 validators.forEach(
function(item){
84 if(!item.func.call(
self, cell, value, item.params)){
87 parameters:item.params
93 return valid.length ? valid :
true;
96 Validate.prototype.validators = {
99 integer:
function(cell, value, parameters){
100 if(value ===
"" || value === null || typeof value ===
"undefined"){
103 value = Number(value);
104 return typeof value ===
'number' && isFinite(value) && Math.floor(value) === value;
108 float:
function(cell, value, parameters){
109 if(value ===
"" || value === null || typeof value ===
"undefined"){
112 value = Number(value);
113 return typeof value ===
'number' && isFinite(value) && value % 1 !== 0;
117 numeric:
function(cell, value, parameters){
118 if(value ===
"" || value === null || typeof value ===
"undefined"){
121 return !isNaN(value);
125 string:
function(cell, value, parameters){
126 if(value ===
"" || value === null || typeof value ===
"undefined"){
133 max:
function(cell, value, parameters){
134 if(value ===
"" || value === null || typeof value ===
"undefined"){
137 return parseFloat(value) <= parameters;
141 min:
function(cell, value, parameters){
142 if(value ===
"" || value === null || typeof value ===
"undefined"){
145 return parseFloat(value) >= parameters;
149 minLength:
function(cell, value, parameters){
150 if(value ===
"" || value === null || typeof value ===
"undefined"){
153 return String(value).length >= parameters;
157 maxLength:
function(cell, value, parameters){
158 if(value ===
"" || value === null || typeof value ===
"undefined"){
161 return String(value).length <= parameters;
165 in:
function(cell, value, parameters){
166 if(value ===
"" || value === null || typeof value ===
"undefined"){
169 if(typeof parameters ==
"string"){
170 parameters = parameters.split(
"|");
173 return value ===
"" || parameters.indexOf(value) > -1;
177 regex:
function(cell, value, parameters){
178 if(value ===
"" || value === null || typeof value ===
"undefined"){
181 var reg =
new RegExp(parameters);
183 return reg.test(value);
187 unique:
function(cell, value, parameters){
188 if(value ===
"" || value === null || typeof value ===
"undefined"){
193 var cellData = cell.getData();
194 var column = cell.getColumn()._getSelf();
196 this.table.rowManager.rows.forEach(
function(row){
197 var data = row.getData();
199 if(data !== cellData){
200 if(value == column.getFieldValue(data)){
210 required:
function(cell, value, parameters){
211 return value !==
"" && value !== null && typeof value !==
"undefined";
216 Tabulator.prototype.registerModule(
"validate", Validate);