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