Upgrade to ExtJS 4.0.2 - Released 06/09/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  * @class Ext.data.validations
18  * @extends Object
19  * 
20  * <p>This singleton contains a set of validation functions that can be used to validate any type
21  * of data. They are most often used in {@link Ext.data.Model Models}, where they are automatically
22  * set up and executed.</p>
23  */
24 Ext.define('Ext.data.validations', {
25     singleton: true,
26     
27     /**
28      * The default error message used when a presence validation fails
29      * @property presenceMessage
30      * @type String
31      */
32     presenceMessage: 'must be present',
33     
34     /**
35      * The default error message used when a length validation fails
36      * @property lengthMessage
37      * @type String
38      */
39     lengthMessage: 'is the wrong length',
40     
41     /**
42      * The default error message used when a format validation fails
43      * @property formatMessage
44      * @type Boolean
45      */
46     formatMessage: 'is the wrong format',
47     
48     /**
49      * The default error message used when an inclusion validation fails
50      * @property inclusionMessage
51      * @type String
52      */
53     inclusionMessage: 'is not included in the list of acceptable values',
54     
55     /**
56      * The default error message used when an exclusion validation fails
57      * @property exclusionMessage
58      * @type String
59      */
60     exclusionMessage: 'is not an acceptable value',
61     
62     /**
63      * Validates that the given value is present
64      * @param {Object} config Optional config object
65      * @param {Mixed} value The value to validate
66      * @return {Boolean} True if validation passed
67      */
68     presence: function(config, value) {
69         if (value === undefined) {
70             value = config;
71         }
72         
73         return !!value;
74     },
75     
76     /**
77      * Returns true if the given value is between the configured min and max values
78      * @param {Object} config Optional config object
79      * @param {String} value The value to validate
80      * @return {Boolean} True if the value passes validation
81      */
82     length: function(config, value) {
83         if (value === undefined) {
84             return false;
85         }
86         
87         var length = value.length,
88             min    = config.min,
89             max    = config.max;
90         
91         if ((min && length < min) || (max && length > max)) {
92             return false;
93         } else {
94             return true;
95         }
96     },
97     
98     /**
99      * Returns true if the given value passes validation against the configured {@link #matcher} regex
100      * @param {Object} config Optional config object
101      * @param {String} value The value to validate
102      * @return {Boolean} True if the value passes the format validation
103      */
104     format: function(config, value) {
105         return !!(config.matcher && config.matcher.test(value));
106     },
107     
108     /**
109      * Validates that the given value is present in the configured {@link #list}
110      * @param {String} value The value to validate
111      * @return {Boolean} True if the value is present in the list
112      */
113     inclusion: function(config, value) {
114         return config.list && Ext.Array.indexOf(config.list,value) != -1;
115     },
116     
117     /**
118      * Validates that the given value is present in the configured {@link #list}
119      * @param {Object} config Optional config object
120      * @param {String} value The value to validate
121      * @return {Boolean} True if the value is not present in the list
122      */
123     exclusion: function(config, value) {
124         return config.list && Ext.Array.indexOf(config.list,value) == -1;
125     }
126 });