PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/js/yii/validators/CValidator.js

http://github.com/phpnode/YiiJS
JavaScript | 271 lines | 120 code | 8 blank | 143 comment | 29 complexity | b0796753139fd146ea012765dca83d39 MD5 | raw file
  1. /*global Yii, php, $, jQuery, alert, clearInterval, clearTimeout, document, event, frames, history, Image, location, name, navigator, Option, parent, screen, setInterval, setTimeout, window, XMLHttpRequest */
  2. /**
  3. * CValidator is the base class for all validators.
  4. *
  5. * Child classes must implement the {@link validateAttribute} method.
  6. *
  7. * The following properties are defined in CValidator:
  8. * <ul>
  9. * <li>{@link attributes}: array, list of attributes to be validated;</li>
  10. * <li>{@link message}: string, the customized error message. The message
  11. * may contain placeholders that will be replaced with the actual content.
  12. * For example, the "{attribute}" placeholder will be replaced with the label
  13. * of the problematic attribute. Different validators may define additional
  14. * placeholders.</li>
  15. * <li>{@link on}: string, in which scenario should the validator be in effect.
  16. * This is used to match the 'on' parameter supplied when calling {@link CModel::validate}.</li>
  17. * </ul>
  18. *
  19. * When using {@link createValidator} to create a validator, the following aliases
  20. * are recognized as the corresponding built-in validator classes:
  21. * <ul>
  22. * <li>required: {@link CRequiredValidator}</li>
  23. * <li>filter: {@link CFilterValidator}</li>
  24. * <li>match: {@link CRegularExpressionValidator}</li>
  25. * <li>email: {@link CEmailValidator}</li>
  26. * <li>url: {@link CUrlValidator}</li>
  27. * <li>unique: {@link CUniqueValidator}</li>
  28. * <li>compare: {@link CCompareValidator}</li>
  29. * <li>length: {@link CStringValidator}</li>
  30. * <li>in: {@link CRangeValidator}</li>
  31. * <li>numerical: {@link CNumberValidator}</li>
  32. * <li>captcha: {@link CCaptchaValidator}</li>
  33. * <li>type: {@link CTypeValidator}</li>
  34. * <li>file: {@link CFileValidator}</li>
  35. * <li>default: {@link CDefaultValueValidator}</li>
  36. * <li>exist: {@link CExistValidator}</li>
  37. * <li>boolean: {@link CBooleanValidator}</li>
  38. * <li>date: {@link CDateValidator}</li>
  39. * <li>safe: {@link CSafeValidator}</li>
  40. * <li>unsafe: {@link CUnsafeValidator}</li>
  41. * </ul>
  42. *
  43. * @originalAuthor Qiang Xue <qiang.xue@gmail.com>
  44. * @version $Id: CValidator.php 3160 2011-04-03 01:08:23Z qiang.xue $
  45. * @package system.validators
  46. * @since 1.0
  47. * @author Charles Pick
  48. * @class
  49. * @extends Yii.CComponent
  50. */
  51. Yii.CValidator = function CValidator() {
  52. };
  53. Yii.CValidator.prototype = new Yii.CComponent();
  54. Yii.CValidator.prototype.constructor = Yii.CValidator;
  55. /**
  56. * @var {Object} list of built-in validators (name=>class)
  57. */
  58. Yii.CValidator.prototype.builtInValidators = {
  59. 'required':'Yii.CRequiredValidator',
  60. 'filter':'Yii.CFilterValidator',
  61. 'match':'Yii.CRegularExpressionValidator',
  62. 'email':'Yii.CEmailValidator',
  63. 'url':'Yii.CUrlValidator',
  64. 'unique':'Yii.CUniqueValidator',
  65. 'compare':'Yii.CCompareValidator',
  66. 'length':'Yii.CStringValidator',
  67. 'in':'Yii.CRangeValidator',
  68. 'numerical':'Yii.CNumberValidator',
  69. 'captcha':'Yii.CCaptchaValidator',
  70. 'type':'Yii.CTypeValidator',
  71. 'file':'Yii.CFileValidator',
  72. 'default':'Yii.CDefaultValueValidator',
  73. 'exist':'Yii.CExistValidator',
  74. 'boolean':'Yii.CBooleanValidator',
  75. 'safe':'Yii.CSafeValidator',
  76. 'unsafe':'Yii.CUnsafeValidator',
  77. 'date':'Yii.CDateValidator'
  78. };
  79. /**
  80. * @var {Array} list of attributes to be validated.
  81. */
  82. Yii.CValidator.prototype.attributes = null;
  83. /**
  84. * @var {String} the user-defined error message. Different validators may define various
  85. * placeholders in the message that are to be replaced with actual values. All validators
  86. * recognize "{attribute}" placeholder, which will be replaced with the label of the attribute.
  87. */
  88. Yii.CValidator.prototype.message = null;
  89. /**
  90. * @var {Boolean} whether this validation rule should be skipped if when there is already a validation
  91. * error for the current attribute. Defaults to false.
  92. * @since 1.1.1
  93. */
  94. Yii.CValidator.prototype.skipOnError = false;
  95. /**
  96. * @var {Array} list of scenarios that the validator should be applied.
  97. * Each array value refers to a scenario name with the same name as its array key.
  98. */
  99. Yii.CValidator.prototype.on = null;
  100. /**
  101. * @var {Boolean} whether attributes listed with this validator should be considered safe for massive assignment.
  102. * Defaults to true.
  103. * @since 1.1.4
  104. */
  105. Yii.CValidator.prototype.safe = true;
  106. /**
  107. * @var {Boolean} whether to perform client-side validation. Defaults to true.
  108. * Please refer to {@link CActiveForm::enableClientValidation} for more details about client-side validation.
  109. * @since 1.1.7
  110. */
  111. Yii.CValidator.prototype.enableClientValidation = true;
  112. /**
  113. * Validates a single attribute.
  114. * This method should be overridden by child classes.
  115. * @param {Yii.CModel} object the data object being validated
  116. * @param {String} attribute the name of the attribute to be validated.
  117. */
  118. Yii.CValidator.prototype.validateAttribute = function (object, attribute) {
  119. };
  120. /**
  121. * Creates a validator object.
  122. * @param {String} name the name or class of the validator
  123. * @param {Yii.CModel} object the data object being validated that may contain the inline validation method
  124. * @param {Mixed} attributes list of attributes to be validated. This can be either an array of
  125. * the attribute names or a string of comma-separated attribute names.
  126. * @param {Array} params initial values to be applied to the validator properties
  127. * @returns {Yii.CValidator} the validator
  128. */
  129. Yii.CValidator.prototype.createValidator = function (name, object, attributes, params) {
  130. var n, on, validator, builtInValidators, className, value, nameParts, i, limit, classObject;
  131. if (params === undefined) {
  132. params = [];
  133. }
  134. if(typeof(attributes) === 'string') {
  135. attributes=attributes.split(/[\s,]+/);
  136. }
  137. if(params.on !== undefined) {
  138. if(Object.prototype.toString.call(params.on) === '[object Array]') {
  139. on=params.on;
  140. }
  141. else {
  142. on=params.on.split(/[\s,]+/);
  143. }
  144. }
  145. else {
  146. on=[];
  147. }
  148. if (object[name] !== undefined && typeof object[name] === "function") {
  149. validator=new Yii.CInlineValidator();
  150. validator.attributes=attributes;
  151. validator.method=name;
  152. validator.params=params;
  153. if(params.skipOnError !== undefined) {
  154. validator.skipOnError=params.skipOnError;
  155. }
  156. }
  157. else {
  158. params.attributes = attributes;
  159. if(this.builtInValidators[name] !== undefined) {
  160. className = this.builtInValidators[name];
  161. }
  162. else {
  163. className = name;
  164. }
  165. if (className.slice(0,3) === "Yii") {
  166. validator=new Yii[className.slice(4)]();
  167. }
  168. else {
  169. validator = new window[className]();
  170. }
  171. for (n in params) {
  172. if (params.hasOwnProperty(n)) {
  173. value = params[n];
  174. validator[n]=value;
  175. }
  176. }
  177. }
  178. validator.on=php.empty(on) ? [] : php.array_combine(on,on);
  179. return validator;
  180. };
  181. /**
  182. * Validates the specified object.
  183. * @param {Yii.CModel} object the data object being validated
  184. * @param {Array} attributes the list of attributes to be validated. Defaults to null,
  185. * meaning every attribute listed in {@link attributes} will be validated.
  186. */
  187. Yii.CValidator.prototype.validate = function (object, attributes) {
  188. var i, attribute, self = this;
  189. if (attributes === undefined) {
  190. attributes = null;
  191. }
  192. if(Object.prototype.toString.call(attributes) === '[object Array]') {
  193. attributes=php.array_intersect(this.attributes,attributes);
  194. }
  195. else {
  196. attributes=this.attributes;
  197. }
  198. Yii.forEach(attributes, function(i,attribute) {
  199. if(!self.skipOnError || !object.hasErrors(attribute)) {
  200. self.validateAttribute(object,attribute);
  201. }
  202. });
  203. };
  204. /**
  205. * Returns the JavaScript needed for performing client-side validation.
  206. * Do not override this method if the validator does not support client-side validation.
  207. * Two predefined JavaScript variables can be used:
  208. * <ul>
  209. * <li>value: the value to be validated</li>
  210. * <li>messages: an array used to hold the validation error messages for the value</li>
  211. * </ul>
  212. * @param {Yii.CModel} object the data object being validated
  213. * @param {String} attribute the name of the attribute to be validated.
  214. * @returns {String} the client-side validation script. Null if the validator does not support client-side validation.
  215. * @see CActiveForm::enableClientValidation
  216. * @since 1.1.7
  217. */
  218. Yii.CValidator.prototype.clientValidateAttribute = function (object, attribute) {
  219. };
  220. /**
  221. * Returns a value indicating whether the validator applies to the specified scenario.
  222. * A validator applies to a scenario as long as any of the following conditions is met:
  223. * <ul>
  224. * <li>the validator's "on" property is empty</li>
  225. * <li>the validator's "on" property contains the specified scenario</li>
  226. * </ul>
  227. * @param {String} scenario scenario name
  228. * @returns {Boolean} whether the validator applies to the specified scenario.
  229. * @since 1.0.2
  230. */
  231. Yii.CValidator.prototype.applyTo = function (scenario) {
  232. return php.empty(this.on) || this.on[scenario] !== undefined;
  233. };
  234. /**
  235. * Adds an error about the specified attribute to the active record.
  236. * This is a helper method that performs message selection and internationalization.
  237. * @param {Yii.CModel} object the data object being validated
  238. * @param {String} attribute the attribute being validated
  239. * @param {String} message the error message
  240. * @param {Array} params values for the placeholders in the error message
  241. */
  242. Yii.CValidator.prototype.addError = function (object, attribute, message, params) {
  243. if (params === undefined) {
  244. params = [];
  245. }
  246. params['{attribute}']=object.getAttributeLabel(attribute);
  247. object.addError(attribute,php.strtr(message,params));
  248. };
  249. /**
  250. * Checks if the given value is empty.
  251. * A value is considered empty if it is null, an empty array, or the trimmed result is an empty string.
  252. * Note that this method is different from PHP empty(). It will return false when the value is 0.
  253. * @param {Mixed} value the value to be checked
  254. * @param {Boolean} trim whether to perform trimming before checking if the string is empty. Defaults to false.
  255. * @returns {Boolean} whether the value is empty
  256. * @since 1.0.9
  257. */
  258. Yii.CValidator.prototype.isEmpty = function (value, trim) {
  259. if (trim === undefined) {
  260. trim = false;
  261. }
  262. return value===null || value===[] || value==='' || trim && (/boolean|number|string/).test(typeof value) && php.trim(value)==='';
  263. }