yii.validation.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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, value) {
  198. var $input = $form.find(attribute.input);
  199. if ($input.is(':checkbox, :radio')) {
  200. return value;
  201. }
  202. value = $input.val();
  203. if (!options.skipOnEmpty || !pub.isEmpty(value)) {
  204. value = $.trim(value);
  205. $input.val(value);
  206. }
  207. return value;
  208. },
  209. captcha: function (value, messages, options) {
  210. if (options.skipOnEmpty && pub.isEmpty(value)) {
  211. return;
  212. }
  213. // CAPTCHA may be updated via AJAX and the updated hash is stored in body data
  214. var hash = $('body').data(options.hashKey);
  215. hash = hash == null ? options.hash : hash[options.caseSensitive ? 0 : 1];
  216. var v = options.caseSensitive ? value : value.toLowerCase();
  217. for (var i = v.length - 1, h = 0; i >= 0; --i) {
  218. h += v.charCodeAt(i);
  219. }
  220. if (h != hash) {
  221. pub.addMessage(messages, options.message, value);
  222. }
  223. },
  224. compare: function (value, messages, options, $form) {
  225. if (options.skipOnEmpty && pub.isEmpty(value)) {
  226. return;
  227. }
  228. var compareValue,
  229. valid = true;
  230. if (options.compareAttribute === undefined) {
  231. compareValue = options.compareValue;
  232. } else {
  233. var $target = $('#' + options.compareAttribute);
  234. if (!$target.length) {
  235. $target = $form.find('[name="' + options.compareAttributeName + '"]');
  236. }
  237. compareValue = $target.val();
  238. }
  239. if (options.type === 'number') {
  240. value = value ? parseFloat(value) : 0;
  241. compareValue = compareValue ? parseFloat(compareValue) : 0;
  242. }
  243. switch (options.operator) {
  244. case '==':
  245. valid = value == compareValue;
  246. break;
  247. case '===':
  248. valid = value === compareValue;
  249. break;
  250. case '!=':
  251. valid = value != compareValue;
  252. break;
  253. case '!==':
  254. valid = value !== compareValue;
  255. break;
  256. case '>':
  257. valid = value > compareValue;
  258. break;
  259. case '>=':
  260. valid = value >= compareValue;
  261. break;
  262. case '<':
  263. valid = value < compareValue;
  264. break;
  265. case '<=':
  266. valid = value <= compareValue;
  267. break;
  268. default:
  269. valid = false;
  270. break;
  271. }
  272. if (!valid) {
  273. pub.addMessage(messages, options.message, value);
  274. }
  275. },
  276. ip: function (value, messages, options) {
  277. if (options.skipOnEmpty && pub.isEmpty(value)) {
  278. return;
  279. }
  280. var negation = null,
  281. cidr = null,
  282. matches = new RegExp(options.ipParsePattern).exec(value);
  283. if (matches) {
  284. negation = matches[1] || null;
  285. value = matches[2];
  286. cidr = matches[4] || null;
  287. }
  288. if (options.subnet === true && cidr === null) {
  289. pub.addMessage(messages, options.messages.noSubnet, value);
  290. return;
  291. }
  292. if (options.subnet === false && cidr !== null) {
  293. pub.addMessage(messages, options.messages.hasSubnet, value);
  294. return;
  295. }
  296. if (options.negation === false && negation !== null) {
  297. pub.addMessage(messages, options.messages.message, value);
  298. return;
  299. }
  300. var ipVersion = value.indexOf(':') === -1 ? 4 : 6;
  301. if (ipVersion == 6) {
  302. if (!(new RegExp(options.ipv6Pattern)).test(value)) {
  303. pub.addMessage(messages, options.messages.message, value);
  304. }
  305. if (!options.ipv6) {
  306. pub.addMessage(messages, options.messages.ipv6NotAllowed, value);
  307. }
  308. } else {
  309. if (!(new RegExp(options.ipv4Pattern)).test(value)) {
  310. pub.addMessage(messages, options.messages.message, value);
  311. }
  312. if (!options.ipv4) {
  313. pub.addMessage(messages, options.messages.ipv4NotAllowed, value);
  314. }
  315. }
  316. }
  317. };
  318. function getUploadedFiles(attribute, messages, options) {
  319. // Skip validation if File API is not available
  320. if (typeof File === "undefined") {
  321. return [];
  322. }
  323. var fileInput = $(attribute.input, attribute.$form).get(0);
  324. // Skip validation if file input does not exist
  325. // (in case file inputs are added dynamically and no file input has been added to the form)
  326. if (typeof fileInput === "undefined") {
  327. return [];
  328. }
  329. var files = fileInput.files;
  330. if (!files) {
  331. messages.push(options.message);
  332. return [];
  333. }
  334. if (files.length === 0) {
  335. if (!options.skipOnEmpty) {
  336. messages.push(options.uploadRequired);
  337. }
  338. return [];
  339. }
  340. if (options.maxFiles && options.maxFiles < files.length) {
  341. messages.push(options.tooMany);
  342. return [];
  343. }
  344. return files;
  345. }
  346. function validateFile(file, messages, options) {
  347. if (options.extensions && options.extensions.length > 0) {
  348. var index = file.name.lastIndexOf('.');
  349. var ext = !~index ? '' : file.name.substr(index + 1, file.name.length).toLowerCase();
  350. if (!~options.extensions.indexOf(ext)) {
  351. messages.push(options.wrongExtension.replace(/\{file\}/g, file.name));
  352. }
  353. }
  354. if (options.mimeTypes && options.mimeTypes.length > 0) {
  355. if (!validateMimeType(options.mimeTypes, file.type)) {
  356. messages.push(options.wrongMimeType.replace(/\{file\}/g, file.name));
  357. }
  358. }
  359. if (options.maxSize && options.maxSize < file.size) {
  360. messages.push(options.tooBig.replace(/\{file\}/g, file.name));
  361. }
  362. if (options.minSize && options.minSize > file.size) {
  363. messages.push(options.tooSmall.replace(/\{file\}/g, file.name));
  364. }
  365. }
  366. function validateMimeType(mimeTypes, fileType) {
  367. for (var i = 0, len = mimeTypes.length; i < len; i++) {
  368. if (new RegExp(mimeTypes[i]).test(fileType)) {
  369. return true;
  370. }
  371. }
  372. return false;
  373. }
  374. function validateImageSize(file, image, messages, options) {
  375. if (options.minWidth && image.width < options.minWidth) {
  376. messages.push(options.underWidth.replace(/\{file\}/g, file.name));
  377. }
  378. if (options.maxWidth && image.width > options.maxWidth) {
  379. messages.push(options.overWidth.replace(/\{file\}/g, file.name));
  380. }
  381. if (options.minHeight && image.height < options.minHeight) {
  382. messages.push(options.underHeight.replace(/\{file\}/g, file.name));
  383. }
  384. if (options.maxHeight && image.height > options.maxHeight) {
  385. messages.push(options.overHeight.replace(/\{file\}/g, file.name));
  386. }
  387. }
  388. return pub;
  389. })(jQuery);