4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-data-validations'>/**
19 </span> * @author Ed Spencer
21 * This singleton contains a set of validation functions that can be used to validate any type of data. They are most
22 * often used in {@link Ext.data.Model Models}, where they are automatically set up and executed.
24 Ext.define('Ext.data.validations', {
27 <span id='Ext-data-validations-property-presenceMessage'> /**
28 </span> * @property {String} presenceMessage
29 * The default error message used when a presence validation fails.
31 presenceMessage: 'must be present',
33 <span id='Ext-data-validations-property-lengthMessage'> /**
34 </span> * @property {String} lengthMessage
35 * The default error message used when a length validation fails.
37 lengthMessage: 'is the wrong length',
39 <span id='Ext-data-validations-property-formatMessage'> /**
40 </span> * @property {Boolean} formatMessage
41 * The default error message used when a format validation fails.
43 formatMessage: 'is the wrong format',
45 <span id='Ext-data-validations-property-inclusionMessage'> /**
46 </span> * @property {String} inclusionMessage
47 * The default error message used when an inclusion validation fails.
49 inclusionMessage: 'is not included in the list of acceptable values',
51 <span id='Ext-data-validations-property-exclusionMessage'> /**
52 </span> * @property {String} exclusionMessage
53 * The default error message used when an exclusion validation fails.
55 exclusionMessage: 'is not an acceptable value',
57 <span id='Ext-data-validations-property-emailMessage'> /**
58 </span> * @property {String} emailMessage
59 * The default error message used when an email validation fails
61 emailMessage: 'is not a valid email address',
63 <span id='Ext-data-validations-property-emailRe'> /**
64 </span> * @property {RegExp} emailRe
65 * The regular expression used to validate email addresses
67 emailRe: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
69 <span id='Ext-data-validations-method-presence'> /**
70 </span> * Validates that the given value is present.
73 * validations: [{type: 'presence', field: 'age'}]
75 * @param {Object} config Config object
76 * @param {Object} value The value to validate
77 * @return {Boolean} True if validation passed
79 presence: function(config, value) {
80 if (value === undefined) {
84 //we need an additional check for zero here because zero is an acceptable form of present data
85 return !!value || value === 0;
88 <span id='Ext-data-validations-method-length'> /**
89 </span> * Returns true if the given value is between the configured min and max values.
92 * validations: [{type: 'length', field: 'name', min: 2}]
94 * @param {Object} config Config object
95 * @param {String} value The value to validate
96 * @return {Boolean} True if the value passes validation
98 length: function(config, value) {
99 if (value === undefined || value === null) {
103 var length = value.length,
107 if ((min && length < min) || (max && length > max)) {
114 <span id='Ext-data-validations-method-email'> /**
115 </span> * Validates that an email string is in the correct format
116 * @param {Object} config Config object
117 * @param {String} email The email address
118 * @return {Boolean} True if the value passes validation
120 email: function(config, email) {
121 return Ext.data.validations.emailRe.test(email);
124 <span id='Ext-data-validations-method-format'> /**
125 </span> * Returns true if the given value passes validation against the configured `matcher` regex.
128 * validations: [{type: 'format', field: 'username', matcher: /([a-z]+)[0-9]{2,3}/}]
130 * @param {Object} config Config object
131 * @param {String} value The value to validate
132 * @return {Boolean} True if the value passes the format validation
134 format: function(config, value) {
135 return !!(config.matcher && config.matcher.test(value));
138 <span id='Ext-data-validations-method-inclusion'> /**
139 </span> * Validates that the given value is present in the configured `list`.
142 * validations: [{type: 'inclusion', field: 'gender', list: ['Male', 'Female']}]
144 * @param {Object} config Config object
145 * @param {String} value The value to validate
146 * @return {Boolean} True if the value is present in the list
148 inclusion: function(config, value) {
149 return config.list && Ext.Array.indexOf(config.list,value) != -1;
152 <span id='Ext-data-validations-method-exclusion'> /**
153 </span> * Validates that the given value is present in the configured `list`.
156 * validations: [{type: 'exclusion', field: 'username', list: ['Admin', 'Operator']}]
158 * @param {Object} config Config object
159 * @param {String} value The value to validate
160 * @return {Boolean} True if the value is not present in the list
162 exclusion: function(config, value) {
163 return config.list && Ext.Array.indexOf(config.list,value) == -1;