Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / data / validations.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @author Ed Spencer
17  *
18  * This singleton contains a set of validation functions that can be used to validate any type of data. They are most
19  * often used in {@link Ext.data.Model Models}, where they are automatically set up and executed.
20  */
21 Ext.define('Ext.data.validations', {
22     singleton: true,
23     
24     /**
25      * @property {String} presenceMessage
26      * The default error message used when a presence validation fails.
27      */
28     presenceMessage: 'must be present',
29     
30     /**
31      * @property {String} lengthMessage
32      * The default error message used when a length validation fails.
33      */
34     lengthMessage: 'is the wrong length',
35     
36     /**
37      * @property {Boolean} formatMessage
38      * The default error message used when a format validation fails.
39      */
40     formatMessage: 'is the wrong format',
41     
42     /**
43      * @property {String} inclusionMessage
44      * The default error message used when an inclusion validation fails.
45      */
46     inclusionMessage: 'is not included in the list of acceptable values',
47     
48     /**
49      * @property {String} exclusionMessage
50      * The default error message used when an exclusion validation fails.
51      */
52     exclusionMessage: 'is not an acceptable value',
53     
54     /**
55      * @property {String} emailMessage
56      * The default error message used when an email validation fails
57      */
58     emailMessage: 'is not a valid email address',
59     
60     /**
61      * @property {RegExp} emailRe
62      * The regular expression used to validate email addresses
63      */
64     emailRe: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
65     
66     /**
67      * Validates that the given value is present.
68      * For example:
69      *
70      *     validations: [{type: 'presence', field: 'age'}]
71      *
72      * @param {Object} config Config object
73      * @param {Object} value The value to validate
74      * @return {Boolean} True if validation passed
75      */
76     presence: function(config, value) {
77         if (value === undefined) {
78             value = config;
79         }
80         
81         //we need an additional check for zero here because zero is an acceptable form of present data
82         return !!value || value === 0;
83     },
84     
85     /**
86      * Returns true if the given value is between the configured min and max values.
87      * For example:
88      *
89      *     validations: [{type: 'length', field: 'name', min: 2}]
90      *
91      * @param {Object} config Config object
92      * @param {String} value The value to validate
93      * @return {Boolean} True if the value passes validation
94      */
95     length: function(config, value) {
96         if (value === undefined || value === null) {
97             return false;
98         }
99         
100         var length = value.length,
101             min    = config.min,
102             max    = config.max;
103         
104         if ((min && length < min) || (max && length > max)) {
105             return false;
106         } else {
107             return true;
108         }
109     },
110     
111     /**
112      * Validates that an email string is in the correct format
113      * @param {Object} config Config object
114      * @param {String} email The email address
115      * @return {Boolean} True if the value passes validation
116      */
117     email: function(config, email) {
118         return Ext.data.validations.emailRe.test(email);
119     },
120     
121     /**
122      * Returns true if the given value passes validation against the configured `matcher` regex.
123      * For example:
124      *
125      *     validations: [{type: 'format', field: 'username', matcher: /([a-z]+)[0-9]{2,3}/}]
126      *
127      * @param {Object} config Config object
128      * @param {String} value The value to validate
129      * @return {Boolean} True if the value passes the format validation
130      */
131     format: function(config, value) {
132         return !!(config.matcher && config.matcher.test(value));
133     },
134     
135     /**
136      * Validates that the given value is present in the configured `list`.
137      * For example:
138      *
139      *     validations: [{type: 'inclusion', field: 'gender', list: ['Male', 'Female']}]
140      *
141      * @param {Object} config Config object
142      * @param {String} value The value to validate
143      * @return {Boolean} True if the value is present in the list
144      */
145     inclusion: function(config, value) {
146         return config.list && Ext.Array.indexOf(config.list,value) != -1;
147     },
148     
149     /**
150      * Validates that the given value is present in the configured `list`.
151      * For example:
152      *
153      *     validations: [{type: 'exclusion', field: 'username', list: ['Admin', 'Operator']}]
154      *
155      * @param {Object} config Config object
156      * @param {String} value The value to validate
157      * @return {Boolean} True if the value is not present in the list
158      */
159     exclusion: function(config, value) {
160         return config.list && Ext.Array.indexOf(config.list,value) == -1;
161     }
162 });