Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / docs / source / validations.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5   <title>The source code</title>
6   <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7   <script type="text/javascript" src="../prettify/prettify.js"></script>
8   <style type="text/css">
9     .highlight { display: block; background-color: #ddd; }
10   </style>
11   <script type="text/javascript">
12     function highlight() {
13       document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
14     }
15   </script>
16 </head>
17 <body onload="prettyPrint(); highlight();">
18   <pre class="prettyprint lang-js"><span id='Ext-data-validations'>/**
19 </span> * @author Ed Spencer
20  * @class Ext.data.validations
21  * @extends Object
22  * 
23  * &lt;p&gt;This singleton contains a set of validation functions that can be used to validate any type
24  * of data. They are most often used in {@link Ext.data.Model Models}, where they are automatically
25  * set up and executed.&lt;/p&gt;
26  */
27 Ext.define('Ext.data.validations', {
28     singleton: true,
29     
30 <span id='Ext-data-validations-property-presenceMessage'>    /**
31 </span>     * The default error message used when a presence validation fails
32      * @property presenceMessage
33      * @type String
34      */
35     presenceMessage: 'must be present',
36     
37 <span id='Ext-data-validations-property-lengthMessage'>    /**
38 </span>     * The default error message used when a length validation fails
39      * @property lengthMessage
40      * @type String
41      */
42     lengthMessage: 'is the wrong length',
43     
44 <span id='Ext-data-validations-property-formatMessage'>    /**
45 </span>     * The default error message used when a format validation fails
46      * @property formatMessage
47      * @type Boolean
48      */
49     formatMessage: 'is the wrong format',
50     
51 <span id='Ext-data-validations-property-inclusionMessage'>    /**
52 </span>     * The default error message used when an inclusion validation fails
53      * @property inclusionMessage
54      * @type String
55      */
56     inclusionMessage: 'is not included in the list of acceptable values',
57     
58 <span id='Ext-data-validations-property-exclusionMessage'>    /**
59 </span>     * The default error message used when an exclusion validation fails
60      * @property exclusionMessage
61      * @type String
62      */
63     exclusionMessage: 'is not an acceptable value',
64     
65 <span id='Ext-data-validations-method-presence'>    /**
66 </span>     * Validates that the given value is present
67      * @param {Object} config Optional config object
68      * @param {Mixed} value The value to validate
69      * @return {Boolean} True if validation passed
70      */
71     presence: function(config, value) {
72         if (value === undefined) {
73             value = config;
74         }
75         
76         return !!value;
77     },
78     
79 <span id='Ext-data-validations-method-length'>    /**
80 </span>     * Returns true if the given value is between the configured min and max values
81      * @param {Object} config Optional config object
82      * @param {String} value The value to validate
83      * @return {Boolean} True if the value passes validation
84      */
85     length: function(config, value) {
86         if (value === undefined) {
87             return false;
88         }
89         
90         var length = value.length,
91             min    = config.min,
92             max    = config.max;
93         
94         if ((min &amp;&amp; length &lt; min) || (max &amp;&amp; length &gt; max)) {
95             return false;
96         } else {
97             return true;
98         }
99     },
100     
101 <span id='Ext-data-validations-method-format'>    /**
102 </span>     * Returns true if the given value passes validation against the configured {@link #matcher} regex
103      * @param {Object} config Optional config object
104      * @param {String} value The value to validate
105      * @return {Boolean} True if the value passes the format validation
106      */
107     format: function(config, value) {
108         return !!(config.matcher &amp;&amp; config.matcher.test(value));
109     },
110     
111 <span id='Ext-data-validations-method-inclusion'>    /**
112 </span>     * Validates that the given value is present in the configured {@link #list}
113      * @param {String} value The value to validate
114      * @return {Boolean} True if the value is present in the list
115      */
116     inclusion: function(config, value) {
117         return config.list &amp;&amp; Ext.Array.indexOf(config.list,value) != -1;
118     },
119     
120 <span id='Ext-data-validations-method-exclusion'>    /**
121 </span>     * Validates that the given value is present in the configured {@link #list}
122      * @param {Object} config Optional config object
123      * @param {String} value The value to validate
124      * @return {Boolean} True if the value is not present in the list
125      */
126     exclusion: function(config, value) {
127         return config.list &amp;&amp; Ext.Array.indexOf(config.list,value) == -1;
128     }
129 });</pre>
130 </body>
131 </html>