Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / docs / source / validations.html
1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-data.validations'>/**
2 </span> * @author Ed Spencer
3  * @class Ext.data.validations
4  * @extends Object
5  * 
6  * &lt;p&gt;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.&lt;/p&gt;
9  */
10 Ext.define('Ext.data.validations', {
11     singleton: true,
12     
13 <span id='Ext-data.validations-property-presenceMessage'>    /**
14 </span>     * The default error message used when a presence validation fails
15      * @property presenceMessage
16      * @type String
17      */
18     presenceMessage: 'must be present',
19     
20 <span id='Ext-data.validations-property-lengthMessage'>    /**
21 </span>     * 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 <span id='Ext-data.validations-property-formatMessage'>    /**
28 </span>     * 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 <span id='Ext-data.validations-property-inclusionMessage'>    /**
35 </span>     * 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 <span id='Ext-data.validations-property-exclusionMessage'>    /**
42 </span>     * 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 <span id='Ext-data.validations-method-presence'>    /**
49 </span>     * 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 <span id='Ext-data.validations-method-length'>    /**
63 </span>     * 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 &amp;&amp; length &lt; min) || (max &amp;&amp; length &gt; max)) {
78             return false;
79         } else {
80             return true;
81         }
82     },
83     
84 <span id='Ext-data.validations-method-format'>    /**
85 </span>     * 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 &amp;&amp; config.matcher.test(value));
92     },
93     
94 <span id='Ext-data.validations-method-inclusion'>    /**
95 </span>     * 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 &amp;&amp; Ext.Array.indexOf(config.list,value) != -1;
101     },
102     
103 <span id='Ext-data.validations-method-exclusion'>    /**
104 </span>     * 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 &amp;&amp; Ext.Array.indexOf(config.list,value) == -1;
111     }
112 });</pre></pre></body></html>