3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
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.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
18 * This singleton contains a set of validation functions that can be used to validate any type of data. They are most
19 * often used in {@link Ext.data.Model Models}, where they are automatically set up and executed.
21 Ext.define('Ext.data.validations', {
25 * @property {String} presenceMessage
26 * The default error message used when a presence validation fails.
28 presenceMessage: 'must be present',
31 * @property {String} lengthMessage
32 * The default error message used when a length validation fails.
34 lengthMessage: 'is the wrong length',
37 * @property {Boolean} formatMessage
38 * The default error message used when a format validation fails.
40 formatMessage: 'is the wrong format',
43 * @property {String} inclusionMessage
44 * The default error message used when an inclusion validation fails.
46 inclusionMessage: 'is not included in the list of acceptable values',
49 * @property {String} exclusionMessage
50 * The default error message used when an exclusion validation fails.
52 exclusionMessage: 'is not an acceptable value',
55 * @property {String} emailMessage
56 * The default error message used when an email validation fails
58 emailMessage: 'is not a valid email address',
61 * @property {RegExp} emailRe
62 * The regular expression used to validate email addresses
64 emailRe: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
67 * Validates that the given value is present.
70 * validations: [{type: 'presence', field: 'age'}]
72 * @param {Object} config Config object
73 * @param {Object} value The value to validate
74 * @return {Boolean} True if validation passed
76 presence: function(config, value) {
77 if (value === undefined) {
81 //we need an additional check for zero here because zero is an acceptable form of present data
82 return !!value || value === 0;
86 * Returns true if the given value is between the configured min and max values.
89 * validations: [{type: 'length', field: 'name', min: 2}]
91 * @param {Object} config Config object
92 * @param {String} value The value to validate
93 * @return {Boolean} True if the value passes validation
95 length: function(config, value) {
96 if (value === undefined || value === null) {
100 var length = value.length,
104 if ((min && length < min) || (max && length > max)) {
112 * Validates that an email string is in the correct format
113 * @param {Object} config Config object
114 * @param {String} email The email address
115 * @return {Boolean} True if the value passes validation
117 email: function(config, email) {
118 return Ext.data.validations.emailRe.test(email);
122 * Returns true if the given value passes validation against the configured `matcher` regex.
125 * validations: [{type: 'format', field: 'username', matcher: /([a-z]+)[0-9]{2,3}/}]
127 * @param {Object} config Config object
128 * @param {String} value The value to validate
129 * @return {Boolean} True if the value passes the format validation
131 format: function(config, value) {
132 return !!(config.matcher && config.matcher.test(value));
136 * Validates that the given value is present in the configured `list`.
139 * validations: [{type: 'inclusion', field: 'gender', list: ['Male', 'Female']}]
141 * @param {Object} config Config object
142 * @param {String} value The value to validate
143 * @return {Boolean} True if the value is present in the list
145 inclusion: function(config, value) {
146 return config.list && Ext.Array.indexOf(config.list,value) != -1;
150 * Validates that the given value is present in the configured `list`.
153 * validations: [{type: 'exclusion', field: 'username', list: ['Admin', 'Operator']}]
155 * @param {Object} config Config object
156 * @param {String} value The value to validate
157 * @return {Boolean} True if the value is not present in the list
159 exclusion: function(config, value) {
160 return config.list && Ext.Array.indexOf(config.list,value) == -1;