]> git.proxmox.com Git - sencha-touch.git/blob - src/src/data/Validations.js
import Sencha Touch 2.4.2 source
[sencha-touch.git] / src / src / data / Validations.js
1 /**
2 * @author Ed Spencer
3 *
4 * This singleton contains a set of validation functions that can be used to validate any type of data. They are most
5 * often used in {@link Ext.data.Model Models}, where they are automatically set up and executed.
6 *
7 * ###Further Reading
8 * [Sencha Touch Data Overview](../../../core_concepts/data/data_package_overview.html)
9 * [Sencha Touch Store Guide](../../../core_concepts/data/stores.html)
10 * [Sencha Touch Models Guide](../../../core_concepts/data/models.html)
11 * [Sencha Touch Proxy Guide](../../../core_concepts/data/proxies.html)
12 */
13 Ext.define('Ext.data.Validations', {
14 alternateClassName: 'Ext.data.validations',
15
16 singleton: true,
17
18 config: {
19 /**
20 * @property {String} presenceMessage
21 * The default error message used when a presence validation fails.
22 */
23 presenceMessage: 'must be present',
24
25 /**
26 * @property {String} lengthMessage
27 * The default error message used when a length validation fails.
28 */
29 lengthMessage: 'is the wrong length',
30
31 /**
32 * @property {String} formatMessage
33 * The default error message used when a format validation fails.
34 */
35 formatMessage: 'is the wrong format',
36
37 /**
38 * @property {String} inclusionMessage
39 * The default error message used when an inclusion validation fails.
40 */
41 inclusionMessage: 'is not included in the list of acceptable values',
42
43 /**
44 * @property {String} exclusionMessage
45 * The default error message used when an exclusion validation fails.
46 */
47 exclusionMessage: 'is not an acceptable value',
48
49 /**
50 * @property {String} emailMessage
51 * The default error message used when an email validation fails
52 */
53 emailMessage: 'is not a valid email address'
54 },
55
56 constructor: function(config) {
57 this.initConfig(config);
58 },
59
60 /**
61 * Returns the configured error message for any of the validation types.
62 * @param {String} type The type of validation you want to get the error message for.
63 * @return {Object}
64 */
65 getMessage: function(type) {
66 var getterFn = this['get' + type[0].toUpperCase() + type.slice(1) + 'Message'];
67 if (getterFn) {
68 return getterFn.call(this);
69 }
70 return '';
71 },
72
73 /**
74 * The regular expression used to validate email addresses
75 * @property emailRe
76 * @type RegExp
77 */
78 emailRe: /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/,
79
80 /**
81 * Validates that the given value is present.
82 * For example:
83 *
84 * validations: [{type: 'presence', field: 'age'}]
85 *
86 * @param {Object} config Config object.
87 * @param {Object} value The value to validate.
88 * @return {Boolean} `true` if validation passed.
89 */
90 presence: function(config, value) {
91 if (arguments.length === 1) {
92 value = config;
93 }
94 return !!value || value === 0;
95 },
96
97 /**
98 * Returns `true` if the given value is between the configured min and max values.
99 * For example:
100 *
101 * validations: [{type: 'length', field: 'name', min: 2}]
102 *
103 * @param {Object} config Config object.
104 * @param {String} value The value to validate.
105 * @return {Boolean} `true` if the value passes validation.
106 */
107 length: function(config, value) {
108 if (value === undefined || value === null) {
109 return false;
110 }
111
112 var length = value.length,
113 min = config.min,
114 max = config.max;
115
116 if ((min && length < min) || (max && length > max)) {
117 return false;
118 } else {
119 return true;
120 }
121 },
122
123 /**
124 * Validates that an email string is in the correct format.
125 * @param {Object} config Config object.
126 * @param {String} email The email address.
127 * @return {Boolean} `true` if the value passes validation.
128 */
129 email: function(config, email) {
130 return Ext.data.validations.emailRe.test(email);
131 },
132
133 /**
134 * Returns `true` if the given value passes validation against the configured `matcher` regex.
135 * For example:
136 *
137 * validations: [{type: 'format', field: 'username', matcher: /([a-z]+)[0-9]{2,3}/}]
138 *
139 * @param {Object} config Config object.
140 * @param {String} value The value to validate.
141 * @return {Boolean} `true` if the value passes the format validation.
142 */
143 format: function(config, value) {
144 if (value === undefined || value === null) {
145 value = '';
146 }
147 return !!(config.matcher && config.matcher.test(value));
148 },
149
150 /**
151 * Validates that the given value is present in the configured `list`.
152 * For example:
153 *
154 * validations: [{type: 'inclusion', field: 'gender', list: ['Male', 'Female']}]
155 *
156 * @param {Object} config Config object.
157 * @param {String} value The value to validate.
158 * @return {Boolean} `true` if the value is present in the list.
159 */
160 inclusion: function(config, value) {
161 return config.list && Ext.Array.indexOf(config.list,value) != -1;
162 },
163
164 /**
165 * Validates that the given value is not present in the configured `list`.
166 *
167 * For example:
168 *
169 * validations: [{type: 'exclusion', field: 'username', list: ['Admin', 'Operator']}]
170 *
171 * @param {Object} config Config object.
172 * @param {String} value The value to validate.
173 * @return {Boolean} `true` if the value is not present in the list.
174 */
175 exclusion: function(config, value) {
176 return config.list && Ext.Array.indexOf(config.list,value) == -1;
177 }
178 });