yii.validation.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /**
  2. * Yii validation module.
  3. *
  4. * This JavaScript module provides the validation methods for the built-in validators.
  5. *
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright (c) 2008 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. * @author Qiang Xue <qiang.xue@gmail.com>
  10. * @since 2.0
  11. */
  12. yii.validation = (function ($) {
  13. var pub = {
  14. isEmpty: function (value) {
  15. return value === null || value === undefined || ($.isArray(value) && value.length === 0) || value === '';
  16. },
  17. addMessage: function (messages, message, value) {
  18. messages.push(message.replace(/\{value\}/g, value));
  19. },
  20. required: function (value, messages, options) {
  21. var valid = false;
  22. if (options.requiredValue === undefined) {
  23. var isString = typeof value == 'string' || value instanceof String;
  24. if (options.strict && value !== undefined || !options.strict && !pub.isEmpty(isString ? $.trim(value) : value)) {
  25. valid = true;
  26. }
  27. } else if (!options.strict && value == options.requiredValue || options.strict && value === options.requiredValue) {
  28. valid = true;
  29. }
  30. if (!valid) {
  31. pub.addMessage(messages, options.message, value);
  32. }
  33. },
  34. // "boolean" is a reserved keyword in older versions of ES so it's quoted for IE < 9 support
  35. 'boolean': function (value, messages, options) {
  36. if (options.skipOnEmpty && pub.isEmpty(value)) {
  37. return;
  38. }
  39. var valid = !options.strict && (value == options.trueValue || value == options.falseValue)
  40. || options.strict && (value === options.trueValue || value === options.falseValue);
  41. if (!valid) {
  42. pub.addMessage(messages, options.message, value);
  43. }
  44. },
  45. string: function (value, messages, options) {
  46. if (options.skipOnEmpty && pub.isEmpty(value)) {
  47. return;
  48. }
  49. if (typeof value !== 'string') {
  50. pub.addMessage(messages, options.message, value);
  51. return;
  52. }
  53. if (options.is !== undefined && value.length != options.is) {
  54. pub.addMessage(messages, options.notEqual, value);
  55. return;
  56. }
  57. if (options.min !== undefined && value.length < options.min) {
  58. pub.addMessage(messages, options.tooShort, value);
  59. }
  60. if (options.max !== undefined && value.length > options.max) {
  61. pub.addMessage(messages, options.tooLong, value);
  62. }
  63. },
  64. file: function (attribute, messages, options) {
  65. var files = getUploadedFiles(attribute, messages, options);
  66. $.each(files, function (i, file) {
  67. validateFile(file, messages, options);
  68. });
  69. },
  70. image: function (attribute, messages, options, deferredList) {
  71. var files = getUploadedFiles(attribute, messages, options);
  72. $.each(files, function (i, file) {
  73. validateFile(file, messages, options);
  74. // Skip image validation if FileReader API is not available
  75. if (typeof FileReader === "undefined") {
  76. return;
  77. }
  78. var deferred = $.Deferred();
  79. pub.validateImage(file, messages, options, deferred, new FileReader(), new Image());
  80. deferredList.push(deferred);
  81. });
  82. },
  83. validateImage: function (file, messages, options, deferred, fileReader, image) {
  84. image.onload = function() {
  85. validateImageSize(file, image, messages, options);
  86. deferred.resolve();
  87. };
  88. image.onerror = function () {
  89. messages.push(options.notImage.replace(/\{file\}/g, file.name));
  90. deferred.resolve();
  91. };
  92. fileReader.onload = function () {
  93. image.src = this.result;
  94. };
  95. // Resolve deferred if there was error while reading data
  96. fileReader.onerror = function () {
  97. deferred.resolve();
  98. };
  99. fileReader.readAsDataURL(file);
  100. },
  101. number: function (value, messages, options) {
  102. if (options.skipOnEmpty && pub.isEmpty(value)) {
  103. return;
  104. }
  105. if (typeof value === 'string' && !options.pattern.test(value)) {
  106. pub.addMessage(messages, options.message, value);
  107. return;
  108. }
  109. if (options.min !== undefined && value < options.min) {
  110. pub.addMessage(messages, options.tooSmall, value);
  111. }
  112. if (options.max !== undefined && value > options.max) {
  113. pub.addMessage(messages, options.tooBig, value);
  114. }
  115. },
  116. range: function (value, messages, options) {
  117. if (options.skipOnEmpty && pub.isEmpty(value)) {
  118. return;
  119. }
  120. if (!options.allowArray && $.isArray(value)) {
  121. pub.addMessage(messages, options.message, value);
  122. return;
  123. }
  124. var inArray = true;
  125. $.each($.isArray(value) ? value : [value], function (i, v) {
  126. if ($.inArray(v, options.range) == -1) {
  127. inArray = false;
  128. return false;
  129. } else {
  130. return true;
  131. }
  132. });
  133. if (options.not === undefined) {
  134. options.not = false;
  135. }
  136. if (options.not === inArray) {
  137. pub.addMessage(messages, options.message, value);
  138. }
  139. },
  140. regularExpression: function (value, messages, options) {
  141. if (options.skipOnEmpty && pub.isEmpty(value)) {
  142. return;
  143. }
  144. if (!options.not && !options.pattern.test(value) || options.not && options.pattern.test(value)) {
  145. pub.addMessage(messages, options.message, value);
  146. }
  147. },
  148. email: function (value, messages, options) {
  149. if (options.skipOnEmpty && pub.isEmpty(value)) {
  150. return;
  151. }
  152. var valid = true,
  153. regexp = /^((?:"?([^"]*)"?\s)?)(?:\s+)?(?:(<?)((.+)@([^>]+))(>?))$/,
  154. matches = regexp.exec(value);
  155. if (matches === null) {
  156. valid = false;
  157. } else {
  158. var localPart = matches[5],
  159. domain = matches[6];
  160. if (options.enableIDN) {
  161. localPart = punycode.toASCII(localPart);
  162. domain = punycode.toASCII(domain);
  163. value = matches[1] + matches[3] + localPart + '@' + domain + matches[7];
  164. }
  165. if (localPart.length > 64) {
  166. valid = false;
  167. } else if ((localPart + '@' + domain).length > 254) {
  168. valid = false;
  169. } else {
  170. valid = options.pattern.test(value) || (options.allowName && options.fullPattern.test(value));
  171. }
  172. }
  173. if (!valid) {
  174. pub.addMessage(messages, options.message, value);
  175. }
  176. },
  177. url: function (value, messages, options) {
  178. if (options.skipOnEmpty && pub.isEmpty(value)) {
  179. return;
  180. }
  181. if (options.defaultScheme && !/:\/\//.test(value)) {
  182. value = options.defaultScheme + '://' + value;
  183. }
  184. var valid = true;
  185. if (options.enableIDN) {
  186. var matches = /^([^:]+):\/\/([^\/]+)(.*)$/.exec(value);
  187. if (matches === null) {
  188. valid = false;
  189. } else {
  190. value = matches[1] + '://' + punycode.toASCII(matches[2]) + matches[3];
  191. }
  192. }
  193. if (!valid || !options.pattern.test(value)) {
  194. pub.addMessage(messages, options.message, value);
  195. }
  196. },
  197. trim: function ($form, attribute, options) {
  198. var $input = $form.find(attribute.input);
  199. var value = $input.val();
  200. if (!options.skipOnEmpty || !pub.isEmpty(value)) {
  201. value = $.trim(value);
  202. $input.val(value);
  203. }
  204. return value;
  205. },
  206. captcha: function (value, messages, options) {
  207. if (options.skipOnEmpty && pub.isEmpty(value)) {
  208. return;
  209. }
  210. // CAPTCHA may be updated via AJAX and the updated hash is stored in body data
  211. var hash = $('body').data(options.hashKey);
  212. hash = hash == null ? options.hash : hash[options.caseSensitive ? 0 : 1];
  213. var v = options.caseSensitive ? value : value.toLowerCase();
  214. for (var i = v.length - 1, h = 0; i >= 0; --i) {
  215. h += v.charCodeAt(i);
  216. }
  217. if (h != hash) {
  218. pub.addMessage(messages, options.message, value);
  219. }
  220. },
  221. compare: function (value, messages, options, $form) {
  222. if (options.skipOnEmpty && pub.isEmpty(value)) {
  223. return;
  224. }
  225. var compareValue,
  226. valid = true;
  227. if (options.compareAttribute === undefined) {
  228. compareValue = options.compareValue;
  229. } else {
  230. var attributes = $form.data('yiiActiveForm').attributes
  231. for (var i = attributes.length - 1; i >= 0; i--) {
  232. if (attributes[i].id === options.compareAttribute) {
  233. compareValue = $(attributes[i].input).val();
  234. }
  235. }
  236. }
  237. if (options.type === 'number') {
  238. value = parseFloat(value);
  239. compareValue = parseFloat(compareValue);
  240. }
  241. switch (options.operator) {
  242. case '==':
  243. valid = value == compareValue;
  244. break;
  245. case '===':
  246. valid = value === compareValue;
  247. break;
  248. case '!=':
  249. valid = value != compareValue;
  250. break;
  251. case '!==':
  252. valid = value !== compareValue;
  253. break;
  254. case '>':
  255. valid = value > compareValue;
  256. break;
  257. case '>=':
  258. valid = value >= compareValue;
  259. break;
  260. case '<':
  261. valid = value < compareValue;
  262. break;
  263. case '<=':
  264. valid = value <= compareValue;
  265. break;
  266. default:
  267. valid = false;
  268. break;
  269. }
  270. if (!valid) {
  271. pub.addMessage(messages, options.message, value);
  272. }
  273. },
  274. ip: function (value, messages, options) {
  275. if (options.skipOnEmpty && pub.isEmpty(value)) {
  276. return;
  277. }
  278. var negation = null,
  279. cidr = null,
  280. matches = new RegExp(options.ipParsePattern).exec(value);
  281. if (matches) {
  282. negation = matches[1] || null;
  283. value = matches[2];
  284. cidr = matches[4] || null;
  285. }
  286. if (options.subnet === true && cidr === null) {
  287. pub.addMessage(messages, options.messages.noSubnet, value);
  288. return;
  289. }
  290. if (options.subnet === false && cidr !== null) {
  291. pub.addMessage(messages, options.messages.hasSubnet, value);
  292. return;
  293. }
  294. if (options.negation === false && negation !== null) {
  295. pub.addMessage(messages, options.messages.message, value);
  296. return;
  297. }
  298. var ipVersion = value.indexOf(':') === -1 ? 4 : 6;
  299. if (ipVersion == 6) {
  300. if (!(new RegExp(options.ipv6Pattern)).test(value)) {
  301. pub.addMessage(messages, options.messages.message, value);
  302. }
  303. if (!options.ipv6) {
  304. pub.addMessage(messages, options.messages.ipv6NotAllowed, value);
  305. }
  306. } else {
  307. if (!(new RegExp(options.ipv4Pattern)).test(value)) {
  308. pub.addMessage(messages, options.messages.message, value);
  309. }
  310. if (!options.ipv4) {
  311. pub.addMessage(messages, options.messages.ipv4NotAllowed, value);
  312. }
  313. }
  314. }
  315. };
  316. function getUploadedFiles(attribute, messages, options) {
  317. // Skip validation if File API is not available
  318. if (typeof File === "undefined") {
  319. return [];
  320. }
  321. var files = $(attribute.input, attribute.$form).get(0).files;
  322. if (!files) {
  323. messages.push(options.message);
  324. return [];
  325. }
  326. if (files.length === 0) {
  327. if (!options.skipOnEmpty) {
  328. messages.push(options.uploadRequired);
  329. }
  330. return [];
  331. }
  332. if (options.maxFiles && options.maxFiles < files.length) {
  333. messages.push(options.tooMany);
  334. return [];
  335. }
  336. return files;
  337. }
  338. function validateFile(file, messages, options) {
  339. if (options.extensions && options.extensions.length > 0) {
  340. var index = file.name.lastIndexOf('.');
  341. var ext = !~index ? '' : file.name.substr(index + 1, file.name.length).toLowerCase();
  342. if (!~options.extensions.indexOf(ext)) {
  343. messages.push(options.wrongExtension.replace(/\{file\}/g, file.name));
  344. }
  345. }
  346. if (options.mimeTypes && options.mimeTypes.length > 0) {
  347. if (!validateMimeType(options.mimeTypes, file.type)) {
  348. messages.push(options.wrongMimeType.replace(/\{file\}/g, file.name));
  349. }
  350. }
  351. if (options.maxSize && options.maxSize < file.size) {
  352. messages.push(options.tooBig.replace(/\{file\}/g, file.name));
  353. }
  354. if (options.minSize && options.minSize > file.size) {
  355. messages.push(options.tooSmall.replace(/\{file\}/g, file.name));
  356. }
  357. }
  358. function validateMimeType(mimeTypes, fileType) {
  359. for (var i = 0, len = mimeTypes.length; i < len; i++) {
  360. if (new RegExp(mimeTypes[i]).test(fileType)) {
  361. return true;
  362. }
  363. }
  364. return false;
  365. }
  366. function validateImageSize(file, image, messages, options) {
  367. if (options.minWidth && image.width < options.minWidth) {
  368. messages.push(options.underWidth.replace(/\{file\}/g, file.name));
  369. }
  370. if (options.maxWidth && image.width > options.maxWidth) {
  371. messages.push(options.overWidth.replace(/\{file\}/g, file.name));
  372. }
  373. if (options.minHeight && image.height < options.minHeight) {
  374. messages.push(options.underHeight.replace(/\{file\}/g, file.name));
  375. }
  376. if (options.maxHeight && image.height > options.maxHeight) {
  377. messages.push(options.overHeight.replace(/\{file\}/g, file.name));
  378. }
  379. }
  380. return pub;
  381. })(jQuery);