PageRenderTime 72ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/extjs/4.2.1/src/data/validations.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 169 lines | 41 code | 16 blank | 112 comment | 15 complexity | 3b7389b2bbf4208c310cc9a39aefbf61 MD5 | raw file
  1. /*
  2. This file is part of Ext JS 4.2
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-05-16 14:36:50 (f9be68accb407158ba2b1be2c226a6ce1f649314)
  14. */
  15. /**
  16. * @author Ed Spencer
  17. *
  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.
  20. */
  21. Ext.define('Ext.data.validations', {
  22. singleton: true,
  23. /**
  24. * @property {String} presenceMessage
  25. * The default error message used when a presence validation fails.
  26. */
  27. presenceMessage: 'must be present',
  28. /**
  29. * @property {String} lengthMessage
  30. * The default error message used when a length validation fails.
  31. */
  32. lengthMessage: 'is the wrong length',
  33. /**
  34. * @property {String} formatMessage
  35. * The default error message used when a format validation fails.
  36. */
  37. formatMessage: 'is the wrong format',
  38. /**
  39. * @property {String} inclusionMessage
  40. * The default error message used when an inclusion validation fails.
  41. */
  42. inclusionMessage: 'is not included in the list of acceptable values',
  43. /**
  44. * @property {String} exclusionMessage
  45. * The default error message used when an exclusion validation fails.
  46. */
  47. exclusionMessage: 'is not an acceptable value',
  48. /**
  49. * @property {String} emailMessage
  50. * The default error message used when an email validation fails
  51. */
  52. emailMessage: 'is not a valid email address',
  53. /**
  54. * @property {RegExp} emailRe
  55. * The regular expression used to validate email addresses
  56. */
  57. emailRe: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
  58. /**
  59. * Validates that the given value is present.
  60. * For example:
  61. *
  62. * validations: [{type: 'presence', field: 'age'}]
  63. *
  64. * @param {Object} config Config object
  65. * @param {Object} value The value to validate
  66. * @return {Boolean} True if validation passed
  67. */
  68. presence: function(config, value) {
  69. // No configs read, so allow just value to be passed
  70. if (arguments.length === 1) {
  71. value = config;
  72. }
  73. //we need an additional check for zero here because zero is an acceptable form of present data
  74. return !!value || value === 0 || value === false;
  75. },
  76. /**
  77. * Returns true if the given value is between the configured min and max values.
  78. * For example:
  79. *
  80. * validations: [{type: 'length', field: 'name', min: 2}]
  81. *
  82. * @param {Object} config Config object
  83. * @param {String} value The value to validate
  84. * @return {Boolean} True if the value passes validation
  85. */
  86. length: function(config, value) {
  87. if (value === undefined || value === null) {
  88. return false;
  89. }
  90. var length = value.length,
  91. min = config.min,
  92. max = config.max;
  93. if ((min && length < min) || (max && length > max)) {
  94. return false;
  95. } else {
  96. return true;
  97. }
  98. },
  99. /**
  100. * Validates that an email string is in the correct format
  101. * @param {Object} config Config object
  102. * @param {String} email The email address
  103. * @return {Boolean} True if the value passes validation
  104. */
  105. email: function(config, email) {
  106. return Ext.data.validations.emailRe.test(email);
  107. },
  108. /**
  109. * Returns true if the given value passes validation against the configured `matcher` regex.
  110. * For example:
  111. *
  112. * validations: [{type: 'format', field: 'username', matcher: /([a-z]+)[0-9]{2,3}/}]
  113. *
  114. * @param {Object} config Config object
  115. * @param {String} value The value to validate
  116. * @return {Boolean} True if the value passes the format validation
  117. */
  118. format: function(config, value) {
  119. return !!(config.matcher && config.matcher.test(value));
  120. },
  121. /**
  122. * Validates that the given value is present in the configured `list`.
  123. * For example:
  124. *
  125. * validations: [{type: 'inclusion', field: 'gender', list: ['Male', 'Female']}]
  126. *
  127. * @param {Object} config Config object
  128. * @param {String} value The value to validate
  129. * @return {Boolean} True if the value is present in the list
  130. */
  131. inclusion: function(config, value) {
  132. return config.list && Ext.Array.indexOf(config.list,value) != -1;
  133. },
  134. /**
  135. * Validates that the given value is not present in the configured `list`.
  136. * For example:
  137. *
  138. * validations: [{type: 'exclusion', field: 'username', list: ['Admin', 'Operator']}]
  139. *
  140. * @param {Object} config Config object
  141. * @param {String} value The value to validate
  142. * @return {Boolean} True if the value is not present in the list
  143. */
  144. exclusion: function(config, value) {
  145. return config.list && Ext.Array.indexOf(config.list,value) == -1;
  146. }
  147. });