validations.html
6.26 KB
1
2
3
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The source code</title>
<link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../resources/prettify/prettify.js"></script>
<style type="text/css">
.highlight { display: block; background-color: #ddd; }
</style>
<script type="text/javascript">
function highlight() {
document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
}
</script>
</head>
<body onload="prettyPrint(); highlight();">
<pre class="prettyprint lang-js"><span id='Ext-data-validations'>/**
</span> * @author Ed Spencer
*
* This singleton contains a set of validation functions that can be used to validate any type of data. They are most
* often used in {@link Ext.data.Model Models}, where they are automatically set up and executed.
*/
Ext.define('Ext.data.validations', {
singleton: true,
<span id='Ext-data-validations-property-presenceMessage'> /**
</span> * @property {String} presenceMessage
* The default error message used when a presence validation fails.
*/
presenceMessage: 'must be present',
<span id='Ext-data-validations-property-lengthMessage'> /**
</span> * @property {String} lengthMessage
* The default error message used when a length validation fails.
*/
lengthMessage: 'is the wrong length',
<span id='Ext-data-validations-property-formatMessage'> /**
</span> * @property {String} formatMessage
* The default error message used when a format validation fails.
*/
formatMessage: 'is the wrong format',
<span id='Ext-data-validations-property-inclusionMessage'> /**
</span> * @property {String} inclusionMessage
* The default error message used when an inclusion validation fails.
*/
inclusionMessage: 'is not included in the list of acceptable values',
<span id='Ext-data-validations-property-exclusionMessage'> /**
</span> * @property {String} exclusionMessage
* The default error message used when an exclusion validation fails.
*/
exclusionMessage: 'is not an acceptable value',
<span id='Ext-data-validations-property-emailMessage'> /**
</span> * @property {String} emailMessage
* The default error message used when an email validation fails
*/
emailMessage: 'is not a valid email address',
<span id='Ext-data-validations-property-emailRe'> /**
</span> * @property {RegExp} emailRe
* The regular expression used to validate email addresses
*/
emailRe: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
<span id='Ext-data-validations-method-presence'> /**
</span> * Validates that the given value is present.
* For example:
*
* validations: [{type: 'presence', field: 'age'}]
*
* @param {Object} config Config object
* @param {Object} value The value to validate
* @return {Boolean} True if validation passed
*/
presence: function(config, value) {
// No configs read, so allow just value to be passed
if (arguments.length === 1) {
value = config;
}
//we need an additional check for zero here because zero is an acceptable form of present data
return !!value || value === 0;
},
<span id='Ext-data-validations-method-length'> /**
</span> * Returns true if the given value is between the configured min and max values.
* For example:
*
* validations: [{type: 'length', field: 'name', min: 2}]
*
* @param {Object} config Config object
* @param {String} value The value to validate
* @return {Boolean} True if the value passes validation
*/
length: function(config, value) {
if (value === undefined || value === null) {
return false;
}
var length = value.length,
min = config.min,
max = config.max;
if ((min && length < min) || (max && length > max)) {
return false;
} else {
return true;
}
},
<span id='Ext-data-validations-method-email'> /**
</span> * Validates that an email string is in the correct format
* @param {Object} config Config object
* @param {String} email The email address
* @return {Boolean} True if the value passes validation
*/
email: function(config, email) {
return Ext.data.validations.emailRe.test(email);
},
<span id='Ext-data-validations-method-format'> /**
</span> * Returns true if the given value passes validation against the configured `matcher` regex.
* For example:
*
* validations: [{type: 'format', field: 'username', matcher: /([a-z]+)[0-9]{2,3}/}]
*
* @param {Object} config Config object
* @param {String} value The value to validate
* @return {Boolean} True if the value passes the format validation
*/
format: function(config, value) {
return !!(config.matcher && config.matcher.test(value));
},
<span id='Ext-data-validations-method-inclusion'> /**
</span> * Validates that the given value is present in the configured `list`.
* For example:
*
* validations: [{type: 'inclusion', field: 'gender', list: ['Male', 'Female']}]
*
* @param {Object} config Config object
* @param {String} value The value to validate
* @return {Boolean} True if the value is present in the list
*/
inclusion: function(config, value) {
return config.list && Ext.Array.indexOf(config.list,value) != -1;
},
<span id='Ext-data-validations-method-exclusion'> /**
</span> * Validates that the given value is not present in the configured `list`.
* For example:
*
* validations: [{type: 'exclusion', field: 'username', list: ['Admin', 'Operator']}]
*
* @param {Object} config Config object
* @param {String} value The value to validate
* @return {Boolean} True if the value is not present in the list
*/
exclusion: function(config, value) {
return config.list && Ext.Array.indexOf(config.list,value) == -1;
}
});</pre>
</body>
</html>