| 1 |
define(["dojo/_base/kernel", "dojo/_base/lang", "./_base" |
| 2 |
], function(kernel, lang, validate){ |
| 3 |
kernel.experimental("dojox.validate.check"); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
validate.check = function(form, profile){ |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
var missing = []; |
| 89 |
var invalid = []; |
| 90 |
|
| 91 |
|
| 92 |
var results = { |
| 93 |
isSuccessful: function() {return ( !this.hasInvalid() && !this.hasMissing() );}, |
| 94 |
hasMissing: function() {return ( missing.length > 0 );}, |
| 95 |
getMissing: function() {return missing;}, |
| 96 |
isMissing: function(elemname) { |
| 97 |
for(var i = 0; i < missing.length; i++){ |
| 98 |
if(elemname == missing[i]){ return true; } |
| 99 |
} |
| 100 |
return false; |
| 101 |
}, |
| 102 |
hasInvalid: function() {return ( invalid.length > 0 );}, |
| 103 |
getInvalid: function() {return invalid;}, |
| 104 |
isInvalid: function(elemname){ |
| 105 |
for(var i = 0; i < invalid.length; i++){ |
| 106 |
if(elemname == invalid[i]){ return true; } |
| 107 |
} |
| 108 |
return false; |
| 109 |
} |
| 110 |
}; |
| 111 |
|
| 112 |
var _undef = function(name,object){ |
| 113 |
return (typeof object[name] == "undefined"); |
| 114 |
}; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
if(profile.trim instanceof Array){ |
| 119 |
for(var i = 0; i < profile.trim.length; i++){ |
| 120 |
var elem = form[profile.trim[i]]; |
| 121 |
if(_undef("type", elem) || elem.type != "text" && elem.type != "textarea" && elem.type != "password"){ continue; } |
| 122 |
elem.value = elem.value.replace(/(^\s*|\s*$)/g, ""); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
if(profile.uppercase instanceof Array){ |
| 127 |
for(var i = 0; i < profile.uppercase.length; i++){ |
| 128 |
var elem = form[profile.uppercase[i]]; |
| 129 |
if(_undef("type", elem) || elem.type != "text" && elem.type != "textarea" && elem.type != "password"){ continue; } |
| 130 |
elem.value = elem.value.toUpperCase(); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
if(profile.lowercase instanceof Array){ |
| 135 |
for (var i = 0; i < profile.lowercase.length; i++){ |
| 136 |
var elem = form[profile.lowercase[i]]; |
| 137 |
if(_undef("type", elem) || elem.type != "text" && elem.type != "textarea" && elem.type != "password"){ continue; } |
| 138 |
elem.value = elem.value.toLowerCase(); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
if(profile.ucfirst instanceof Array){ |
| 143 |
for(var i = 0; i < profile.ucfirst.length; i++){ |
| 144 |
var elem = form[profile.ucfirst[i]]; |
| 145 |
if(_undef("type", elem) || elem.type != "text" && elem.type != "textarea" && elem.type != "password"){ continue; } |
| 146 |
elem.value = elem.value.replace(/\b\w+\b/g, function(word) { return word.substring(0,1).toUpperCase() + word.substring(1).toLowerCase(); }); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
if(profile.digit instanceof Array){ |
| 151 |
for(var i = 0; i < profile.digit.length; i++){ |
| 152 |
var elem = form[profile.digit[i]]; |
| 153 |
if(_undef("type", elem) || elem.type != "text" && elem.type != "textarea" && elem.type != "password"){ continue; } |
| 154 |
elem.value = elem.value.replace(/\D/g, ""); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
if(profile.required instanceof Array){ |
| 160 |
for(var i = 0; i < profile.required.length; i++){ |
| 161 |
if(!lang.isString(profile.required[i])){ continue; } |
| 162 |
var elem = form[profile.required[i]]; |
| 163 |
|
| 164 |
if(!_undef("type", elem) |
| 165 |
&& (elem.type == "text" || elem.type == "textarea" || elem.type == "password" || elem.type == "file") |
| 166 |
&& /^\s*$/.test(elem.value)){ |
| 167 |
missing[missing.length] = elem.name; |
| 168 |
} |
| 169 |
|
| 170 |
else if(!_undef("type", elem) && (elem.type == "select-one" || elem.type == "select-multiple") |
| 171 |
&& (elem.selectedIndex == -1 |
| 172 |
|| /^\s*$/.test(elem.options[elem.selectedIndex].value))){ |
| 173 |
missing[missing.length] = elem.name; |
| 174 |
} |
| 175 |
|
| 176 |
else if(elem instanceof Array){ |
| 177 |
var checked = false; |
| 178 |
for(var j = 0; j < elem.length; j++){ |
| 179 |
if (elem[j].checked) { checked = true; } |
| 180 |
} |
| 181 |
if(!checked){ |
| 182 |
missing[missing.length] = elem[0].name; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
if(profile.required instanceof Array){ |
| 190 |
for (var i = 0; i < profile.required.length; i++){ |
| 191 |
if(!lang.isObject(profile.required[i])){ continue; } |
| 192 |
var elem, numRequired; |
| 193 |
for(var name in profile.required[i]){ |
| 194 |
elem = form[name]; |
| 195 |
numRequired = profile.required[i][name]; |
| 196 |
} |
| 197 |
|
| 198 |
if(elem instanceof Array){ |
| 199 |
var checked = 0; |
| 200 |
for(var j = 0; j < elem.length; j++){ |
| 201 |
if(elem[j].checked){ checked++; } |
| 202 |
} |
| 203 |
if(checked < numRequired){ |
| 204 |
missing[missing.length] = elem[0].name; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
else if(!_undef("type", elem) && elem.type == "select-multiple" ){ |
| 209 |
var selected = 0; |
| 210 |
for(var j = 0; j < elem.options.length; j++){ |
| 211 |
if (elem.options[j].selected && !/^\s*$/.test(elem.options[j].value)) { selected++; } |
| 212 |
} |
| 213 |
if(selected < numRequired){ |
| 214 |
missing[missing.length] = elem.name; |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
if(lang.isObject(profile.dependencies)){ |
| 225 |
|
| 226 |
for(name in profile.dependencies){ |
| 227 |
var elem = form[name]; |
| 228 |
if(_undef("type", elem)){continue;} |
| 229 |
if(elem.type != "text" && elem.type != "textarea" && elem.type != "password"){ continue; } |
| 230 |
if(/\S+/.test(elem.value)){ continue; } |
| 231 |
if(results.isMissing(elem.name)){ continue; } |
| 232 |
var target = form[profile.dependencies[name]]; |
| 233 |
if(target.type != "text" && target.type != "textarea" && target.type != "password"){ continue; } |
| 234 |
if(/^\s*$/.test(target.value)){ continue; } |
| 235 |
missing[missing.length] = elem.name; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
if(lang.isObject(profile.constraints)){ |
| 241 |
|
| 242 |
for(name in profile.constraints){ |
| 243 |
var elem = form[name]; |
| 244 |
if(!elem) {continue;} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
if(!_undef("tagName",elem) |
| 249 |
&& (elem.tagName.toLowerCase().indexOf("input") >= 0 |
| 250 |
|| elem.tagName.toLowerCase().indexOf("textarea") >= 0) |
| 251 |
&& /^\s*$/.test(elem.value)){ |
| 252 |
continue; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
var constraintResponse; |
| 257 |
|
| 258 |
if(lang.isFunction(profile.constraints[name])){ |
| 259 |
constraintResponse = profile.constraints[name](elem.value); |
| 260 |
}else if(lang.isFunction(lang.getObject(name, false, profile.constraints))){ |
| 261 |
|
| 262 |
constraintResponse = lang.getObject(name, false, profile.constraints)(elem.value); |
| 263 |
}else if(lang.isArray(profile.constraints[name])){ |
| 264 |
|
| 265 |
|
| 266 |
if(lang.isArray(profile.constraints[name][0])){ |
| 267 |
for(var i=0; i<profile.constraints[name].length; i++){ |
| 268 |
constraintResponse = validate.evaluateConstraint(profile, profile.constraints[name][i], name, elem); |
| 269 |
if(!constraintResponse.isValid){ break; } |
| 270 |
} |
| 271 |
}else{ |
| 272 |
|
| 273 |
|
| 274 |
if(lang.isFunction(lang.getObject(name, false, profile.constraints))){ |
| 275 |
constraintResponse = validate.evaluateConstraint(profile, profile.constraints[profile.constraints[name]], name, elem); |
| 276 |
}else{ |
| 277 |
constraintResponse = validate.evaluateConstraint(profile, profile.constraints[name], name, elem); |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
if(!constraintResponse){ |
| 284 |
invalid[invalid.length] = elem.name; |
| 285 |
}else if(typeof constraintResponse !== "boolean" && !constraintResponse.isValid){ |
| 286 |
invalid[invalid.length] = { field : elem.name, message : constraintResponse.message }; |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
if(lang.isObject(profile.confirm)){ |
| 293 |
for(name in profile.confirm){ |
| 294 |
var elem = form[name]; |
| 295 |
var target = form[profile.confirm[name]]; |
| 296 |
if (_undef("type", elem) || _undef("type", target) || (elem.type != "text" && elem.type != "textarea" && elem.type != "password") |
| 297 |
||(target.type != elem.type) |
| 298 |
||(target.value == elem.value) |
| 299 |
||(results.isInvalid(elem.name)) |
| 300 |
||(/^\s*$/.test(target.value))) |
| 301 |
{ |
| 302 |
continue; |
| 303 |
} |
| 304 |
invalid[invalid.length] = elem.name; |
| 305 |
} |
| 306 |
} |
| 307 |
return results; |
| 308 |
}; |
| 309 |
|
| 310 |
|
| 311 |
validate.evaluateConstraint=function(profile, constraint, fieldName, elem){ |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
var isValidSomething = constraint[0]; |
| 336 |
var params = constraint.slice(1); |
| 337 |
params.unshift(elem.value); |
| 338 |
if(typeof isValidSomething != "undefined" && typeof isValidSomething != "string"){ |
| 339 |
return isValidSomething.apply(null, params); |
| 340 |
}else if(typeof isValidSomething != "undefined" && typeof isValidSomething == "string"){ |
| 341 |
if(lang.isFunction(lang.getObject(isValidSomething))){ |
| 342 |
return lang.getObject(isValidSomething).apply(null, params); |
| 343 |
} |
| 344 |
} |
| 345 |
return false; |
| 346 |
}; |
| 347 |
|
| 348 |
return validate.check; |
| 349 |
}); |