Subversion Repositories Applications.papyrus

Rev

Rev 1318 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.validate.check");
14
dojo.require("dojo.validate.common");
15
dojo.require("dojo.lang.common");
16
dojo.validate.check = function (form, profile) {
17
	var missing = [];
18
	var invalid = [];
19
	var results = {isSuccessful:function () {
20
		return (!this.hasInvalid() && !this.hasMissing());
21
	}, hasMissing:function () {
22
		return (missing.length > 0);
23
	}, getMissing:function () {
24
		return missing;
25
	}, isMissing:function (elemname) {
26
		for (var i = 0; i < missing.length; i++) {
27
			if (elemname == missing[i]) {
28
				return true;
29
			}
30
		}
31
		return false;
32
	}, hasInvalid:function () {
33
		return (invalid.length > 0);
34
	}, getInvalid:function () {
35
		return invalid;
36
	}, isInvalid:function (elemname) {
37
		for (var i = 0; i < invalid.length; i++) {
38
			if (elemname == invalid[i]) {
39
				return true;
40
			}
41
		}
42
		return false;
43
	}};
44
	if (profile.trim instanceof Array) {
45
		for (var i = 0; i < profile.trim.length; i++) {
46
			var elem = form[profile.trim[i]];
47
			if (dj_undef("type", elem) || elem.type != "text" && elem.type != "textarea" && elem.type != "password") {
48
				continue;
49
			}
50
			elem.value = elem.value.replace(/(^\s*|\s*$)/g, "");
51
		}
52
	}
53
	if (profile.uppercase instanceof Array) {
54
		for (var i = 0; i < profile.uppercase.length; i++) {
55
			var elem = form[profile.uppercase[i]];
56
			if (dj_undef("type", elem) || elem.type != "text" && elem.type != "textarea" && elem.type != "password") {
57
				continue;
58
			}
59
			elem.value = elem.value.toUpperCase();
60
		}
61
	}
62
	if (profile.lowercase instanceof Array) {
63
		for (var i = 0; i < profile.lowercase.length; i++) {
64
			var elem = form[profile.lowercase[i]];
65
			if (dj_undef("type", elem) || elem.type != "text" && elem.type != "textarea" && elem.type != "password") {
66
				continue;
67
			}
68
			elem.value = elem.value.toLowerCase();
69
		}
70
	}
71
	if (profile.ucfirst instanceof Array) {
72
		for (var i = 0; i < profile.ucfirst.length; i++) {
73
			var elem = form[profile.ucfirst[i]];
74
			if (dj_undef("type", elem) || elem.type != "text" && elem.type != "textarea" && elem.type != "password") {
75
				continue;
76
			}
77
			elem.value = elem.value.replace(/\b\w+\b/g, function (word) {
78
				return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase();
79
			});
80
		}
81
	}
82
	if (profile.digit instanceof Array) {
83
		for (var i = 0; i < profile.digit.length; i++) {
84
			var elem = form[profile.digit[i]];
85
			if (dj_undef("type", elem) || elem.type != "text" && elem.type != "textarea" && elem.type != "password") {
86
				continue;
87
			}
88
			elem.value = elem.value.replace(/\D/g, "");
89
		}
90
	}
91
	if (profile.required instanceof Array) {
92
		for (var i = 0; i < profile.required.length; i++) {
93
			if (!dojo.lang.isString(profile.required[i])) {
94
				continue;
95
			}
96
			var elem = form[profile.required[i]];
97
			if (!dj_undef("type", elem) && (elem.type == "text" || elem.type == "textarea" || elem.type == "password") && /^\s*$/.test(elem.value)) {
98
				missing[missing.length] = elem.name;
99
			} else {
100
				if (!dj_undef("type", elem) && (elem.type == "select-one" || elem.type == "select-multiple") && (elem.selectedIndex == -1 || /^\s*$/.test(elem.options[elem.selectedIndex].value))) {
101
					missing[missing.length] = elem.name;
102
				} else {
103
					if (elem instanceof Array) {
104
						var checked = false;
105
						for (var j = 0; j < elem.length; j++) {
106
							if (elem[j].checked) {
107
								checked = true;
108
							}
109
						}
110
						if (!checked) {
111
							missing[missing.length] = elem[0].name;
112
						}
113
					}
114
				}
115
			}
116
		}
117
	}
118
	if (profile.required instanceof Array) {
119
		for (var i = 0; i < profile.required.length; i++) {
120
			if (!dojo.lang.isObject(profile.required[i])) {
121
				continue;
122
			}
123
			var elem, numRequired;
124
			for (var name in profile.required[i]) {
125
				elem = form[name];
126
				numRequired = profile.required[i][name];
127
			}
128
			if (elem instanceof Array) {
129
				var checked = 0;
130
				for (var j = 0; j < elem.length; j++) {
131
					if (elem[j].checked) {
132
						checked++;
133
					}
134
				}
135
				if (checked < numRequired) {
136
					missing[missing.length] = elem[0].name;
137
				}
138
			} else {
139
				if (!dj_undef("type", elem) && elem.type == "select-multiple") {
140
					var selected = 0;
141
					for (var j = 0; j < elem.options.length; j++) {
142
						if (elem.options[j].selected && !/^\s*$/.test(elem.options[j].value)) {
143
							selected++;
144
						}
145
					}
146
					if (selected < numRequired) {
147
						missing[missing.length] = elem.name;
148
					}
149
				}
150
			}
151
		}
152
	}
153
	if (dojo.lang.isObject(profile.dependencies) || dojo.lang.isObject(profile.dependancies)) {
154
		if (profile["dependancies"]) {
155
			dojo.deprecated("dojo.validate.check", "profile 'dependancies' is deprecated, please use " + "'dependencies'", "0.5");
156
			profile.dependencies = profile.dependancies;
157
		}
158
		for (name in profile.dependencies) {
159
			var elem = form[name];
160
			if (dj_undef("type", elem)) {
161
				continue;
162
			}
163
			if (elem.type != "text" && elem.type != "textarea" && elem.type != "password") {
164
				continue;
165
			}
166
			if (/\S+/.test(elem.value)) {
167
				continue;
168
			}
169
			if (results.isMissing(elem.name)) {
170
				continue;
171
			}
172
			var target = form[profile.dependencies[name]];
173
			if (target.type != "text" && target.type != "textarea" && target.type != "password") {
174
				continue;
175
			}
176
			if (/^\s*$/.test(target.value)) {
177
				continue;
178
			}
179
			missing[missing.length] = elem.name;
180
		}
181
	}
182
	if (dojo.lang.isObject(profile.constraints)) {
183
		for (name in profile.constraints) {
184
			var elem = form[name];
185
			if (!elem) {
186
				continue;
187
			}
188
			if (!dj_undef("tagName", elem) && (elem.tagName.toLowerCase().indexOf("input") >= 0 || elem.tagName.toLowerCase().indexOf("textarea") >= 0) && /^\s*$/.test(elem.value)) {
189
				continue;
190
			}
191
			var isValid = true;
192
			if (dojo.lang.isFunction(profile.constraints[name])) {
193
				isValid = profile.constraints[name](elem.value);
194
			} else {
195
				if (dojo.lang.isArray(profile.constraints[name])) {
196
					if (dojo.lang.isArray(profile.constraints[name][0])) {
197
						for (var i = 0; i < profile.constraints[name].length; i++) {
198
							isValid = dojo.validate.evaluateConstraint(profile, profile.constraints[name][i], name, elem);
199
							if (!isValid) {
200
								break;
201
							}
202
						}
203
					} else {
204
						isValid = dojo.validate.evaluateConstraint(profile, profile.constraints[name], name, elem);
205
					}
206
				}
207
			}
208
			if (!isValid) {
209
				invalid[invalid.length] = elem.name;
210
			}
211
		}
212
	}
213
	if (dojo.lang.isObject(profile.confirm)) {
214
		for (name in profile.confirm) {
215
			var elem = form[name];
216
			var target = form[profile.confirm[name]];
217
			if (dj_undef("type", elem) || dj_undef("type", target) || (elem.type != "text" && elem.type != "textarea" && elem.type != "password") || (target.type != elem.type) || (target.value == elem.value) || (results.isInvalid(elem.name)) || (/^\s*$/.test(target.value))) {
218
				continue;
219
			}
220
			invalid[invalid.length] = elem.name;
221
		}
222
	}
223
	return results;
224
};
225
dojo.validate.evaluateConstraint = function (profile, constraint, fieldName, elem) {
226
	var isValidSomething = constraint[0];
227
	var params = constraint.slice(1);
228
	params.unshift(elem.value);
229
	if (typeof isValidSomething != "undefined") {
230
		return isValidSomething.apply(null, params);
231
	}
232
	return false;
233
};
234