validations.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  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; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-data-validations'>/**
  19. </span> * @author Ed Spencer
  20. *
  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.
  23. */
  24. Ext.define('Ext.data.validations', {
  25. singleton: true,
  26. <span id='Ext-data-validations-property-presenceMessage'> /**
  27. </span> * @property {String} presenceMessage
  28. * The default error message used when a presence validation fails.
  29. */
  30. presenceMessage: 'must be present',
  31. <span id='Ext-data-validations-property-lengthMessage'> /**
  32. </span> * @property {String} lengthMessage
  33. * The default error message used when a length validation fails.
  34. */
  35. lengthMessage: 'is the wrong length',
  36. <span id='Ext-data-validations-property-formatMessage'> /**
  37. </span> * @property {String} formatMessage
  38. * The default error message used when a format validation fails.
  39. */
  40. formatMessage: 'is the wrong format',
  41. <span id='Ext-data-validations-property-inclusionMessage'> /**
  42. </span> * @property {String} inclusionMessage
  43. * The default error message used when an inclusion validation fails.
  44. */
  45. inclusionMessage: 'is not included in the list of acceptable values',
  46. <span id='Ext-data-validations-property-exclusionMessage'> /**
  47. </span> * @property {String} exclusionMessage
  48. * The default error message used when an exclusion validation fails.
  49. */
  50. exclusionMessage: 'is not an acceptable value',
  51. <span id='Ext-data-validations-property-emailMessage'> /**
  52. </span> * @property {String} emailMessage
  53. * The default error message used when an email validation fails
  54. */
  55. emailMessage: 'is not a valid email address',
  56. <span id='Ext-data-validations-property-emailRe'> /**
  57. </span> * @property {RegExp} emailRe
  58. * The regular expression used to validate email addresses
  59. */
  60. emailRe: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
  61. <span id='Ext-data-validations-method-presence'> /**
  62. </span> * Validates that the given value is present.
  63. * For example:
  64. *
  65. * validations: [{type: 'presence', field: 'age'}]
  66. *
  67. * @param {Object} config Config object
  68. * @param {Object} value The value to validate
  69. * @return {Boolean} True if validation passed
  70. */
  71. presence: function(config, value) {
  72. // No configs read, so allow just value to be passed
  73. if (arguments.length === 1) {
  74. value = config;
  75. }
  76. //we need an additional check for zero here because zero is an acceptable form of present data
  77. return !!value || value === 0;
  78. },
  79. <span id='Ext-data-validations-method-length'> /**
  80. </span> * Returns true if the given value is between the configured min and max values.
  81. * For example:
  82. *
  83. * validations: [{type: 'length', field: 'name', min: 2}]
  84. *
  85. * @param {Object} config Config object
  86. * @param {String} value The value to validate
  87. * @return {Boolean} True if the value passes validation
  88. */
  89. length: function(config, value) {
  90. if (value === undefined || value === null) {
  91. return false;
  92. }
  93. var length = value.length,
  94. min = config.min,
  95. max = config.max;
  96. if ((min &amp;&amp; length &lt; min) || (max &amp;&amp; length &gt; max)) {
  97. return false;
  98. } else {
  99. return true;
  100. }
  101. },
  102. <span id='Ext-data-validations-method-email'> /**
  103. </span> * Validates that an email string is in the correct format
  104. * @param {Object} config Config object
  105. * @param {String} email The email address
  106. * @return {Boolean} True if the value passes validation
  107. */
  108. email: function(config, email) {
  109. return Ext.data.validations.emailRe.test(email);
  110. },
  111. <span id='Ext-data-validations-method-format'> /**
  112. </span> * Returns true if the given value passes validation against the configured `matcher` regex.
  113. * For example:
  114. *
  115. * validations: [{type: 'format', field: 'username', matcher: /([a-z]+)[0-9]{2,3}/}]
  116. *
  117. * @param {Object} config Config object
  118. * @param {String} value The value to validate
  119. * @return {Boolean} True if the value passes the format validation
  120. */
  121. format: function(config, value) {
  122. return !!(config.matcher &amp;&amp; config.matcher.test(value));
  123. },
  124. <span id='Ext-data-validations-method-inclusion'> /**
  125. </span> * Validates that the given value is present in the configured `list`.
  126. * For example:
  127. *
  128. * validations: [{type: 'inclusion', field: 'gender', list: ['Male', 'Female']}]
  129. *
  130. * @param {Object} config Config object
  131. * @param {String} value The value to validate
  132. * @return {Boolean} True if the value is present in the list
  133. */
  134. inclusion: function(config, value) {
  135. return config.list &amp;&amp; Ext.Array.indexOf(config.list,value) != -1;
  136. },
  137. <span id='Ext-data-validations-method-exclusion'> /**
  138. </span> * Validates that the given value is not present in the configured `list`.
  139. * For example:
  140. *
  141. * validations: [{type: 'exclusion', field: 'username', list: ['Admin', 'Operator']}]
  142. *
  143. * @param {Object} config Config object
  144. * @param {String} value The value to validate
  145. * @return {Boolean} True if the value is not present in the list
  146. */
  147. exclusion: function(config, value) {
  148. return config.list &amp;&amp; Ext.Array.indexOf(config.list,value) == -1;
  149. }
  150. });</pre>
  151. </body>
  152. </html>