]>
git.proxmox.com Git - sencha-touch.git/blob - src/src/data/Validations.js
4 * This singleton contains a set of validation functions that can be used to validate any type of data. They are most
5 * often used in {@link Ext.data.Model Models}, where they are automatically set up and executed.
8 * [Sencha Touch Data Overview](../../../core_concepts/data/data_package_overview.html)
9 * [Sencha Touch Store Guide](../../../core_concepts/data/stores.html)
10 * [Sencha Touch Models Guide](../../../core_concepts/data/models.html)
11 * [Sencha Touch Proxy Guide](../../../core_concepts/data/proxies.html)
13 Ext
.define('Ext.data.Validations', {
14 alternateClassName
: 'Ext.data.validations',
20 * @property {String} presenceMessage
21 * The default error message used when a presence validation fails.
23 presenceMessage
: 'must be present',
26 * @property {String} lengthMessage
27 * The default error message used when a length validation fails.
29 lengthMessage
: 'is the wrong length',
32 * @property {String} formatMessage
33 * The default error message used when a format validation fails.
35 formatMessage
: 'is the wrong format',
38 * @property {String} inclusionMessage
39 * The default error message used when an inclusion validation fails.
41 inclusionMessage
: 'is not included in the list of acceptable values',
44 * @property {String} exclusionMessage
45 * The default error message used when an exclusion validation fails.
47 exclusionMessage
: 'is not an acceptable value',
50 * @property {String} emailMessage
51 * The default error message used when an email validation fails
53 emailMessage
: 'is not a valid email address'
56 constructor: function(config
) {
57 this.initConfig(config
);
61 * Returns the configured error message for any of the validation types.
62 * @param {String} type The type of validation you want to get the error message for.
65 getMessage: function(type
) {
66 var getterFn
= this['get' + type
[0].toUpperCase() + type
.slice(1) + 'Message'];
68 return getterFn
.call(this);
74 * The regular expression used to validate email addresses
78 emailRe
: /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/,
81 * Validates that the given value is present.
84 * validations: [{type: 'presence', field: 'age'}]
86 * @param {Object} config Config object.
87 * @param {Object} value The value to validate.
88 * @return {Boolean} `true` if validation passed.
90 presence: function(config
, value
) {
91 if (arguments
.length
=== 1) {
94 return !!value
|| value
=== 0;
98 * Returns `true` if the given value is between the configured min and max values.
101 * validations: [{type: 'length', field: 'name', min: 2}]
103 * @param {Object} config Config object.
104 * @param {String} value The value to validate.
105 * @return {Boolean} `true` if the value passes validation.
107 length: function(config
, value
) {
108 if (value
=== undefined || value
=== null) {
112 var length
= value
.length
,
116 if ((min
&& length
< min
) || (max
&& length
> max
)) {
124 * Validates that an email string is in the correct format.
125 * @param {Object} config Config object.
126 * @param {String} email The email address.
127 * @return {Boolean} `true` if the value passes validation.
129 email: function(config
, email
) {
130 return Ext
.data
.validations
.emailRe
.test(email
);
134 * Returns `true` if the given value passes validation against the configured `matcher` regex.
137 * validations: [{type: 'format', field: 'username', matcher: /([a-z]+)[0-9]{2,3}/}]
139 * @param {Object} config Config object.
140 * @param {String} value The value to validate.
141 * @return {Boolean} `true` if the value passes the format validation.
143 format: function(config
, value
) {
144 if (value
=== undefined || value
=== null) {
147 return !!(config
.matcher
&& config
.matcher
.test(value
));
151 * Validates that the given value is present in the configured `list`.
154 * validations: [{type: 'inclusion', field: 'gender', list: ['Male', 'Female']}]
156 * @param {Object} config Config object.
157 * @param {String} value The value to validate.
158 * @return {Boolean} `true` if the value is present in the list.
160 inclusion: function(config
, value
) {
161 return config
.list
&& Ext
.Array
.indexOf(config
.list
,value
) != -1;
165 * Validates that the given value is not present in the configured `list`.
169 * validations: [{type: 'exclusion', field: 'username', list: ['Admin', 'Operator']}]
171 * @param {Object} config Config object.
172 * @param {String} value The value to validate.
173 * @return {Boolean} `true` if the value is not present in the list.
175 exclusion: function(config
, value
) {
176 return config
.list
&& Ext
.Array
.indexOf(config
.list
,value
) == -1;