PageRenderTime 42ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/assets/javascripts/vendor/backbone.validation.js

https://github.com/wagnerpv/iugu-ux
JavaScript | 660 lines | 399 code | 92 blank | 169 comment | 87 complexity | a41aec7651038ff02cf93734cca6fcb1 MD5 | raw file
  1. Backbone.Validation = (function(_){
  2. 'use strict';
  3. // Default options
  4. // ---------------
  5. var defaultOptions = {
  6. forceUpdate: false,
  7. selector: 'name',
  8. labelFormatter: 'internationalized',
  9. valid: Function.prototype,
  10. invalid: Function.prototype
  11. };
  12. // Helper functions
  13. // ----------------
  14. // Formatting functions used for formatting error messages
  15. var formatFunctions = {
  16. // Uses the configured label formatter to format the attribute name
  17. // to make it more readable for the user
  18. formatLabel: function(attrName, model) {
  19. return defaultLabelFormatters[defaultOptions.labelFormatter](attrName, model);
  20. },
  21. // Replaces nummeric placeholders like {0} in a string with arguments
  22. // passed to the function
  23. format: function() {
  24. var args = Array.prototype.slice.call(arguments),
  25. text = _t(args.shift());
  26. return text.replace(/\{(\d+)\}/g, function(match, number) {
  27. return typeof args[number] !== 'undefined' ? args[number] : match;
  28. });
  29. }
  30. };
  31. // Flattens an object
  32. // eg:
  33. //
  34. // var o = {
  35. // address: {
  36. // street: 'Street',
  37. // zip: 1234
  38. // }
  39. // };
  40. //
  41. // becomes:
  42. //
  43. // var o = {
  44. // 'address.street': 'Street',
  45. // 'address.zip': 1234
  46. // };
  47. var flatten = function (obj, into, prefix) {
  48. into = into || {};
  49. prefix = prefix || '';
  50. _.each(obj, function(val, key) {
  51. if(obj.hasOwnProperty(key)) {
  52. if (val && typeof val === 'object' && !(val instanceof Date || val instanceof RegExp || val instanceof Backbone.Collection)) {
  53. flatten(val, into, prefix + key + '.');
  54. }
  55. else {
  56. into[prefix + key] = val;
  57. }
  58. }
  59. });
  60. return into;
  61. };
  62. // Validation
  63. // ----------
  64. var Validation = (function(){
  65. // Returns an object with undefined properties for all
  66. // attributes on the model that has defined one or more
  67. // validation rules.
  68. var getValidatedAttrs = function(model) {
  69. return _.reduce(_.keys(model.validation || {}), function(memo, key) {
  70. memo[key] = void 0;
  71. return memo;
  72. }, {});
  73. };
  74. // Looks on the model for validations for a specified
  75. // attribute. Returns an array of any validators defined,
  76. // or an empty array if none is defined.
  77. var getValidators = function(model, attr) {
  78. var attrValidationSet = model.validation ? model.validation[attr] || {} : {};
  79. // If the validator is a function or a string, wrap it in a function validator
  80. if (_.isFunction(attrValidationSet) || _.isString(attrValidationSet)) {
  81. attrValidationSet = {
  82. fn: attrValidationSet
  83. };
  84. }
  85. // Stick the validator object into an array
  86. if(!_.isArray(attrValidationSet)) {
  87. attrValidationSet = [attrValidationSet];
  88. }
  89. // Reduces the array of validators into a new array with objects
  90. // with a validation method to call, the value to validate against
  91. // and the specified error message, if any
  92. return _.reduce(attrValidationSet, function(memo, attrValidation) {
  93. _.each(_.without(_.keys(attrValidation), 'msg'), function(validator) {
  94. memo.push({
  95. fn: defaultValidators[validator],
  96. val: attrValidation[validator],
  97. msg: attrValidation.msg instanceof Function ? attrValidation.msg() : attrValidation.msg
  98. });
  99. });
  100. return memo;
  101. }, []);
  102. };
  103. // Validates an attribute against all validators defined
  104. // for that attribute. If one or more errors are found,
  105. // the first error message is returned.
  106. // If the attribute is valid, an empty string is returned.
  107. var validateAttr = function(model, attr, value, computed) {
  108. // Reduces the array of validators to an error message by
  109. // applying all the validators and returning the first error
  110. // message, if any.
  111. return _.reduce(getValidators(model, attr), function(memo, validator){
  112. // Pass the format functions plus the default
  113. // validators as the context to the validator
  114. var ctx = _.extend({}, formatFunctions, defaultValidators),
  115. result = validator.fn.call(ctx, value, attr, validator.val, model, computed);
  116. if(result === false || memo === false) {
  117. return false;
  118. }
  119. if (result && !memo) {
  120. return validator.msg || result;
  121. }
  122. return memo;
  123. }, '');
  124. };
  125. var validateNestedCollection = function(collection) {
  126. var errors = {};
  127. _.each(collection.models, function(model) {
  128. var validatedAttrs = getValidatedAttrs(model);
  129. var allAttrs = _.extend({}, validatedAttrs, model.attributes);
  130. var error = validateModel(model, allAttrs);
  131. if (!error.isValid) {
  132. errors[model.cid] = error;
  133. }
  134. });
  135. if (_.isEmpty(errors))
  136. errors = false;
  137. return errors;
  138. }
  139. // Loops through the model's attributes and validates them all.
  140. // Returns and object containing names of invalid attributes
  141. // as well as error messages.
  142. var validateModel = function(model, attrs) {
  143. var error,
  144. invalidAttrs = {},
  145. isValid = true,
  146. computed = _.clone(attrs),
  147. flattened = flatten(attrs);
  148. _.each(flattened, function(val, attr) {
  149. if(val instanceof Backbone.Collection) {
  150. error = validateNestedCollection(val);
  151. }
  152. else {
  153. error = validateAttr(model, attr, val, computed);
  154. }
  155. if (error) {
  156. invalidAttrs[attr] = error;
  157. isValid = false;
  158. }
  159. });
  160. return {
  161. invalidAttrs: invalidAttrs,
  162. isValid: isValid
  163. };
  164. };
  165. // Contains the methods that are mixed in on the model when binding
  166. var mixin = function(view, options) {
  167. return {
  168. // Check whether or not a value passes validation
  169. // without updating the model
  170. preValidate: function(attr, value) {
  171. return validateAttr(this, attr, value, _.extend({}, this.attributes));
  172. },
  173. // Check to see if an attribute, an array of attributes or the
  174. // entire model is valid. Passing true will force a validation
  175. // of the model.
  176. isValid: function(option) {
  177. var flattened = flatten(this.attributes);
  178. if(_.isString(option)){
  179. return !validateAttr(this, option, flattened[option], _.extend({}, this.attributes));
  180. }
  181. if(_.isArray(option)){
  182. return _.reduce(option, function(memo, attr) {
  183. return memo && !validateAttr(this, attr, flattened[attr], _.extend({}, this.attributes));
  184. }, true, this);
  185. }
  186. if(option === true) {
  187. this.validate();
  188. }
  189. return this.validation ? this._isValid : true;
  190. },
  191. callNestedValidForCollection: function(attribute, collection, opt, result) {
  192. _.each(collection.models, function(model) {
  193. var nestedValidatedAttrs = getValidatedAttrs(model);
  194. _.each(nestedValidatedAttrs, function(error, attr) {
  195. _.each(result, function(res, cid) {
  196. var invalid = res.hasOwnProperty(attr);
  197. if(!invalid) {
  198. opt.valid(view, attribute + '.' + attr, opt.selector, cid);
  199. }
  200. });
  201. });
  202. });
  203. },
  204. callNestedInvalidForCollection: function(attribute, collection, opt, result) {
  205. _.each(collection.models, function(model) {
  206. var nestedValidatedAttrs = getValidatedAttrs(model);
  207. _.each(nestedValidatedAttrs, function(error, attr) {
  208. _.each(result, function(res, cid) {
  209. var invalid = res.invalidAttrs.hasOwnProperty(attr);
  210. if(invalid) {
  211. opt.invalid(view, attribute + '.' + attr, res.invalidAttrs[attr], opt.selector, cid);
  212. }
  213. });
  214. });
  215. });
  216. },
  217. // This is called by Backbone when it needs to perform validation.
  218. // You can call it manually without any parameters to validate the
  219. // entire model.
  220. validate: function(attrs, setOptions){
  221. var model = this,
  222. validateAll = !attrs,
  223. opt = _.extend({}, options, setOptions),
  224. validatedAttrs = getValidatedAttrs(model),
  225. allAttrs = _.extend({}, validatedAttrs, model.attributes, attrs),
  226. changedAttrs = flatten(attrs || allAttrs),
  227. result = validateModel(model, allAttrs);
  228. model._isValid = result.isValid;
  229. // After validation is performed, loop through all changed attributes
  230. // and call the valid callbacks so the view is updated.
  231. _.each(validatedAttrs, function(val, attr){
  232. var invalid = result.invalidAttrs.hasOwnProperty(attr);
  233. if(!invalid){
  234. opt.valid(view, attr, opt.selector, null);
  235. } else if(changedAttrs[attr] instanceof Backbone.Collection) {
  236. model.callNestedValidForCollection(attr, changedAttrs[attr], opt, result.invalidAttrs[attr]);
  237. }
  238. });
  239. // After validation is performed, loop through all changed attributes
  240. // and call the invalid callback so the view is updated.
  241. _.each(validatedAttrs, function(val, attr){
  242. var invalid = result.invalidAttrs.hasOwnProperty(attr),
  243. changed = changedAttrs.hasOwnProperty(attr);
  244. if(invalid && changedAttrs[attr] instanceof Backbone.Collection) {
  245. model.callNestedInvalidForCollection(attr, changedAttrs[attr], opt, result.invalidAttrs[attr]);
  246. }
  247. else if(invalid && (changed || validateAll)){
  248. opt.invalid(view, attr, result.invalidAttrs[attr], opt.selector);
  249. }
  250. });
  251. // Trigger validated events.
  252. // Need to defer this so the model is actually updated before
  253. // the event is triggered.
  254. _.defer(function() {
  255. model.trigger('validated', model._isValid, model, result.invalidAttrs);
  256. model.trigger('validated:' + (model._isValid ? 'valid' : 'invalid'), model, result.invalidAttrs);
  257. });
  258. // Return any error messages to Backbone, unless the forceUpdate flag is set.
  259. // Then we do not return anything and fools Backbone to believe the validation was
  260. // a success. That way Backbone will update the model regardless.
  261. if (!opt.forceUpdate && _.intersection(_.keys(result.invalidAttrs), _.keys(changedAttrs)).length > 0) {
  262. return result.invalidAttrs;
  263. }
  264. }
  265. };
  266. };
  267. // Helper to mix in validation on a model
  268. var bindModel = function(view, model, options) {
  269. _.extend(model, mixin(view, options));
  270. };
  271. // Removes the methods added to a model
  272. var unbindModel = function(model) {
  273. delete model.validate;
  274. delete model.preValidate;
  275. delete model.isValid;
  276. };
  277. // Mix in validation on a model whenever a model is
  278. // added to a collection
  279. var collectionAdd = function(model) {
  280. bindModel(this.view, model, this.options);
  281. };
  282. // Remove validation from a model whenever a model is
  283. // removed from a collection
  284. var collectionRemove = function(model) {
  285. unbindModel(model);
  286. };
  287. // Returns the public methods on Backbone.Validation
  288. return {
  289. // Current version of the library
  290. version: '0.7.1',
  291. // Called to configure the default options
  292. configure: function(options) {
  293. _.extend(defaultOptions, options);
  294. },
  295. // Hooks up validation on a view with a model
  296. // or collection
  297. bind: function(view, options) {
  298. var model = view.model,
  299. collection = view.collection;
  300. options = _.extend({}, defaultOptions, defaultCallbacks, options);
  301. if(typeof model === 'undefined' && typeof collection === 'undefined'){
  302. throw 'Before you execute the binding your view must have a model or a collection.\n' +
  303. 'See http://thedersen.com/projects/backbone-validation/#using-form-model-validation for more information.';
  304. }
  305. if(model) {
  306. bindModel(view, model, options);
  307. }
  308. else if(collection) {
  309. collection.each(function(model){
  310. bindModel(view, model, options);
  311. });
  312. collection.bind('add', collectionAdd, {view: view, options: options});
  313. collection.bind('remove', collectionRemove);
  314. }
  315. },
  316. // Removes validation from a view with a model
  317. // or collection
  318. unbind: function(view) {
  319. var model = view.model,
  320. collection = view.collection;
  321. if(model) {
  322. unbindModel(view.model);
  323. }
  324. if(collection) {
  325. collection.each(function(model){
  326. unbindModel(model);
  327. });
  328. collection.unbind('add', collectionAdd);
  329. collection.unbind('remove', collectionRemove);
  330. }
  331. },
  332. // Used to extend the Backbone.Model.prototype
  333. // with validation
  334. mixin: mixin(null, defaultOptions)
  335. };
  336. }());
  337. // Callbacks
  338. // ---------
  339. var defaultCallbacks = Validation.callbacks = {
  340. // Gets called when a previously invalid field in the
  341. // view becomes valid. Removes any error message.
  342. // Should be overridden with custom functionality.
  343. valid: function(view, attr, selector, cid) {
  344. var sel = '[' + selector + '~="' + attr + '"]';
  345. if (cid)
  346. sel = sel + '[cid="' + cid + '"]';
  347. view.$(sel)
  348. .removeClass('invalid')
  349. .removeAttr('data-error');
  350. },
  351. // Gets called when a field in the view becomes invalid.
  352. // Adds a error message.
  353. // Should be overridden with custom functionality.
  354. invalid: function(view, attr, error, selector, cid) {
  355. var sel = '[' + selector + '~="' + attr + '"]';
  356. if (cid)
  357. sel = sel + '[cid="' + cid + '"]';
  358. view.$(sel)
  359. .addClass('invalid')
  360. .attr('data-error', error);
  361. }
  362. };
  363. // Patterns
  364. // --------
  365. var defaultPatterns = Validation.patterns = {
  366. // Matches any digit(s) (i.e. 0-9)
  367. digits: /^\d+$/,
  368. // Matched any number (e.g. 100.000)
  369. number: /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/,
  370. // Matches a valid email address (e.g. mail@example.com)
  371. email: /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i,
  372. // Mathes any valid url (e.g. http://www.xample.com)
  373. url: /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i
  374. };
  375. // Error messages
  376. // --------------
  377. // Error message for the build in validators.
  378. // {x} gets swapped out with arguments form the validator.
  379. var defaultMessages = Validation.messages = {
  380. required: 'validation_messages.required',
  381. acceptance: 'validation_messages.acceptance',
  382. min: 'validation_messages.min',
  383. max: 'validation_messages.max',
  384. range: 'validation_messages.range',
  385. length: 'validation_messages.length',
  386. minLength: 'validation_messages.minLength',
  387. maxLength: 'validation_messages.maxLength',
  388. rangeLength: 'validation_messages.rangeLength',
  389. oneOf: 'validation_messages.oneOf',
  390. equalTo: 'validation_messages.equalTo',
  391. pattern: 'validation_messages.pattern'
  392. };
  393. // Label formatters
  394. // ----------------
  395. // Label formatters are used to convert the attribute name
  396. // to a more human friendly label when using the built in
  397. // error messages.
  398. // Configure which one to use with a call to
  399. //
  400. // Backbone.Validation.configure({
  401. // labelFormatter: 'label'
  402. // });
  403. var defaultLabelFormatters = Validation.labelFormatters = {
  404. // Returns the attribute name with applying any formatting
  405. none: function(attrName) {
  406. return attrName;
  407. },
  408. // Converts attributeName or attribute_name to Attribute name
  409. sentenceCase: function(attrName) {
  410. return attrName.replace(/(?:^\w|[A-Z]|\b\w)/g, function(match, index) {
  411. return index === 0 ? match.toUpperCase() : ' ' + match.toLowerCase();
  412. }).replace('_', ' ');
  413. },
  414. // Looks for a label configured on the model and returns it
  415. //
  416. // var Model = Backbone.Model.extend({
  417. // validation: {
  418. // someAttribute: {
  419. // required: true
  420. // }
  421. // },
  422. //
  423. // labels: {
  424. // someAttribute: 'Custom label'
  425. // }
  426. // });
  427. label: function(attrName, model) {
  428. return (model.labels && model.labels[attrName]) || defaultLabelFormatters.sentenceCase(attrName, model);
  429. },
  430. internationalized: function(attrName, model) {
  431. var right_attr = attrName.split('.');
  432. right_attr = right_attr[right_attr.length-1];
  433. return _t(model.identifier + '_fields.' + right_attr);
  434. }
  435. };
  436. // Built in validators
  437. // -------------------
  438. var defaultValidators = Validation.validators = (function(){
  439. // Use native trim when defined
  440. var trim = String.prototype.trim ?
  441. function(text) {
  442. return text === null ? '' : String.prototype.trim.call(text);
  443. } :
  444. function(text) {
  445. var trimLeft = /^\s+/,
  446. trimRight = /\s+$/;
  447. return text === null ? '' : text.toString().replace(trimLeft, '').replace(trimRight, '');
  448. };
  449. // Determines whether or not a value is a number
  450. var isNumber = function(value){
  451. return _.isNumber(value) || (_.isString(value) && value.match(defaultPatterns.number));
  452. };
  453. // Determines whether or not not a value is empty
  454. var hasValue = function(value) {
  455. return !(_.isNull(value) || _.isUndefined(value) || (_.isString(value) && trim(value) === ''));
  456. };
  457. return {
  458. // Function validator
  459. // Lets you implement a custom function used for validation
  460. fn: function(value, attr, fn, model, computed) {
  461. if(_.isString(fn)){
  462. fn = model[fn];
  463. }
  464. return fn.call(model, value, attr, computed);
  465. },
  466. // Required validator
  467. // Validates if the attribute is required or not
  468. required: function(value, attr, required, model, computed) {
  469. var isRequired = _.isFunction(required) ? required.call(model, value, attr, computed) : required;
  470. if(!isRequired && !hasValue(value)) {
  471. return false; // overrides all other validators
  472. }
  473. if (isRequired && !hasValue(value)) {
  474. return this.format(defaultMessages.required, this.formatLabel(attr, model));
  475. }
  476. },
  477. // Acceptance validator
  478. // Validates that something has to be accepted, e.g. terms of use
  479. // `true` or 'true' are valid
  480. acceptance: function(value, attr, accept, model) {
  481. if(value !== 'true' && (!_.isBoolean(value) || value === false)) {
  482. return this.format(defaultMessages.acceptance, this.formatLabel(attr, model));
  483. }
  484. },
  485. // Min validator
  486. // Validates that the value has to be a number and equal to or greater than
  487. // the min value specified
  488. min: function(value, attr, minValue, model) {
  489. if (!isNumber(value) || value < minValue) {
  490. return this.format(defaultMessages.min, this.formatLabel(attr, model), minValue);
  491. }
  492. },
  493. // Max validator
  494. // Validates that the value has to be a number and equal to or less than
  495. // the max value specified
  496. max: function(value, attr, maxValue, model) {
  497. if (!isNumber(value) || value > maxValue) {
  498. return this.format(defaultMessages.max, this.formatLabel(attr, model), maxValue);
  499. }
  500. },
  501. // Range validator
  502. // Validates that the value has to be a number and equal to or between
  503. // the two numbers specified
  504. range: function(value, attr, range, model) {
  505. if(!isNumber(value) || value < range[0] || value > range[1]) {
  506. return this.format(defaultMessages.range, this.formatLabel(attr, model), range[0], range[1]);
  507. }
  508. },
  509. // Length validator
  510. // Validates that the value has to be a string with length equal to
  511. // the length value specified
  512. length: function(value, attr, length, model) {
  513. if (!hasValue(value) || trim(value).length !== length) {
  514. return this.format(defaultMessages.length, this.formatLabel(attr, model), length);
  515. }
  516. },
  517. // Min length validator
  518. // Validates that the value has to be a string with length equal to or greater than
  519. // the min length value specified
  520. minLength: function(value, attr, minLength, model) {
  521. if (!hasValue(value) || trim(value).length < minLength) {
  522. return this.format(defaultMessages.minLength, this.formatLabel(attr, model), minLength);
  523. }
  524. },
  525. // Max length validator
  526. // Validates that the value has to be a string with length equal to or less than
  527. // the max length value specified
  528. maxLength: function(value, attr, maxLength, model) {
  529. if (!hasValue(value) || trim(value).length > maxLength) {
  530. return this.format(defaultMessages.maxLength, this.formatLabel(attr, model), maxLength);
  531. }
  532. },
  533. // Range length validator
  534. // Validates that the value has to be a string and equal to or between
  535. // the two numbers specified
  536. rangeLength: function(value, attr, range, model) {
  537. if(!hasValue(value) || trim(value).length < range[0] || trim(value).length > range[1]) {
  538. return this.format(defaultMessages.rangeLength, this.formatLabel(attr, model), range[0], range[1]);
  539. }
  540. },
  541. // One of validator
  542. // Validates that the value has to be equal to one of the elements in
  543. // the specified array. Case sensitive matching
  544. oneOf: function(value, attr, values, model) {
  545. if(!_.include(values, value)){
  546. return this.format(defaultMessages.oneOf, this.formatLabel(attr, model), values.join(', '));
  547. }
  548. },
  549. // Equal to validator
  550. // Validates that the value has to be equal to the value of the attribute
  551. // with the name specified
  552. equalTo: function(value, attr, equalTo, model, computed) {
  553. if(value !== computed[equalTo]) {
  554. return this.format(defaultMessages.equalTo, this.formatLabel(attr, model), this.formatLabel(equalTo, model));
  555. }
  556. },
  557. // Pattern validator
  558. // Validates that the value has to match the pattern specified.
  559. // Can be a regular expression or the name of one of the built in patterns
  560. pattern: function(value, attr, pattern, model) {
  561. if (!hasValue(value) || !value.toString().match(defaultPatterns[pattern] || pattern)) {
  562. return this.format(defaultMessages.pattern, this.formatLabel(attr, model), pattern);
  563. }
  564. }
  565. };
  566. }());
  567. return Validation;
  568. }(_));