backbone-validation
Advanced tools
Comparing version 0.6.3 to 0.6.4
@@ -1,2 +0,2 @@ | ||
// Backbone.Validation v0.6.3 | ||
// Backbone.Validation v0.6.4 | ||
// | ||
@@ -8,2 +8,2 @@ // Copyright (c) 2011-2012 Thomas Pedersen | ||
// http://thedersen.com/projects/backbone-validation | ||
(function(e){typeof exports=="object"?module.exports=e(require("backbone"),require("underscore")):typeof define=="function"&&define.amd&&define(["backbone","underscore"],e)})(function(e,t){return e.Validation=function(e){"use strict";var t={forceUpdate:!1,selector:"name",labelFormatter:"sentenceCase",valid:Function.prototype,invalid:Function.prototype},n=function(){var n=function(t){return e.reduce(e.keys(t.validation||{}),function(e,t){return e[t]=void 0,e},{})},i=function(t,n){var r=t.validation?t.validation[n]||{}:{};if(e.isFunction(r)||e.isString(r))r={fn:r};return e.isArray(r)||(r=[r]),e.reduce(r,function(t,n){return e.each(e.without(e.keys(n),"msg"),function(e){t.push({fn:u[e],val:n[e],msg:n.msg})}),t},[])},s=function(t,n,r,s){return e.reduce(i(t,n),function(e,i){var o=i.fn.call(u,r,n,i.val,t,s);return o===!1||e===!1?!1:o&&!e?i.msg||o:e},"")},o=function(t,n){var r,i,o={},u=!0,a=e.clone(n);for(i in n)r=s(t,i,n[i],a),r&&(o[i]=r,u=!1);return{invalidAttrs:o,isValid:u}},a=function(t,r){return{preValidate:function(t,n){return s(this,t,n,e.extend({},this.attributes))},isValid:function(t){if(e.isString(t))return!s(this,t,this.get(t),e.extend({},this.attributes));if(e.isArray(t)){for(var n=0;n<t.length;n++)if(s(this,t[n],this.get(t[n]),e.extend({},this.attributes)))return!1;return!0}return t===!0&&this.validate(),this.validation?this._isValid:!0},validate:function(i,s){var u=this,a=!i,f=e.extend({},r,s),l=n(u),c=e.extend({},l,u.attributes,i),h=i||c,p=o(u,c);u._isValid=p.isValid,e.each(e.keys(l),function(e){var n=p.invalidAttrs.hasOwnProperty(e);n||f.valid(t,e,f.selector)}),e.each(e.keys(l),function(e){var n=p.invalidAttrs.hasOwnProperty(e),r=h.hasOwnProperty(e);n&&(r||a)&&f.invalid(t,e,p.invalidAttrs[e],f.selector)}),e.defer(function(){u.trigger("validated",u._isValid,u,p.invalidAttrs),u.trigger("validated:"+(u._isValid?"valid":"invalid"),u,p.invalidAttrs)});if(!f.forceUpdate&&e.intersection(e.keys(p.invalidAttrs),e.keys(h)).length>0)return p.invalidAttrs}}},f=function(t,n,r){e.extend(n,a(t,r))},l=function(e){delete e.validate,delete e.preValidate,delete e.isValid},c=function(e){f(this.view,e,this.options)},h=function(e){l(e)};return{version:"0.6.3",configure:function(n){e.extend(t,n)},bind:function(n,i){var s=n.model,o=n.collection;i=e.extend({},t,r,i);if(typeof s=="undefined"&&typeof o=="undefined")throw"Before you execute the binding your view must have a model or a collection.\nSee http://thedersen.com/projects/backbone-validation/#using-form-model-validation for more information.";s&&f(n,s,i),o&&(o.each(function(e){f(n,e,i)}),o.bind("add",c,{view:n,options:i}),o.bind("remove",h))},unbind:function(e){var t=e.model,n=e.collection;t&&l(e.model),n&&(n.each(function(e){l(e)}),n.unbind("add",c),n.unbind("remove",h))},mixin:a(null,t)}}(),r=n.callbacks={valid:function(e,t,n){e.$("["+n+"~="+t+"]").removeClass("invalid").removeAttr("data-error")},invalid:function(e,t,n,r){e.$("["+r+"~="+t+"]").addClass("invalid").attr("data-error",n)}},i=n.patterns={digits:/^\d+$/,number:/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/,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,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},s=n.messages={required:"{0} is required",acceptance:"{0} must be accepted",min:"{0} must be greater than or equal to {1}",max:"{0} must be less than or equal to {1}",range:"{0} must be between {1} and {2}",length:"{0} must be {1} characters",minLength:"{0} must be at least {1} characters",maxLength:"{0} must be at most {1} characters",rangeLength:"{0} must be between {1} and {2} characters",oneOf:"{0} must be one of: {1}",equalTo:"{0} must be the same as {1}",pattern:"{0} must be a valid {1}"},o=n.labelFormatters={none:function(e){return e},sentenceCase:function(e){return e.replace(/(?:^\w|[A-Z]|\b\w)/g,function(e,t){return t===0?e.toUpperCase():" "+e.toLowerCase()}).replace("_"," ")},label:function(e,t){return t.labels&&t.labels[e]||o.sentenceCase(e,t)}},u=n.validators=function(){var n=String.prototype.trim?function(e){return e===null?"":String.prototype.trim.call(e)}:function(e){var t=/^\s+/,n=/\s+$/;return e===null?"":e.toString().replace(t,"").replace(n,"")},r=function(e,n){return o[t.labelFormatter](e,n)},u=function(){var e=Array.prototype.slice.call(arguments),t=e.shift();return t.replace(/\{(\d+)\}/g,function(t,n){return typeof e[n]!="undefined"?e[n]:t})},a=function(t){return e.isNumber(t)||e.isString(t)&&t.match(i.number)},f=function(t){return!(e.isNull(t)||e.isUndefined(t)||e.isString(t)&&n(t)==="")};return{fn:function(t,n,r,i,s){return e.isString(r)&&(r=i[r]),r.call(i,t,n,s)},required:function(t,n,i,o,a){var l=e.isFunction(i)?i.call(o,t,n,a):i;if(!l&&!f(t))return!1;if(l&&!f(t))return u(s.required,r(n,o))},acceptance:function(t,n,i,o){if(t!=="true"&&(!e.isBoolean(t)||t===!1))return u(s.acceptance,r(n,o))},min:function(e,t,n,i){if(!a(e)||e<n)return u(s.min,r(t,i),n)},max:function(e,t,n,i){if(!a(e)||e>n)return u(s.max,r(t,i),n)},range:function(e,t,n,i){if(!a(e)||e<n[0]||e>n[1])return u(s.range,r(t,i),n[0],n[1])},length:function(e,t,i,o){if(!f(e)||n(e).length!==i)return u(s.length,r(t,o),i)},minLength:function(e,t,i,o){if(!f(e)||n(e).length<i)return u(s.minLength,r(t,o),i)},maxLength:function(e,t,i,o){if(!f(e)||n(e).length>i)return u(s.maxLength,r(t,o),i)},rangeLength:function(e,t,i,o){if(!f(e)||n(e).length<i[0]||n(e).length>i[1])return u(s.rangeLength,r(t,o),i[0],i[1])},oneOf:function(t,n,i,o){if(!e.include(i,t))return u(s.oneOf,r(n,o),i.join(", "))},equalTo:function(e,t,n,i,o){if(e!==o[n])return u(s.equalTo,r(t,i),r(n,i))},pattern:function(e,t,n,o){if(!f(e)||!e.toString().match(i[n]||n))return u(s.pattern,r(t,o),n)}}}();return n}(t),e.Validation}); | ||
(function(e){typeof exports=="object"?module.exports=e(require("backbone"),require("underscore")):typeof define=="function"&&define.amd&&define(["backbone","underscore"],e)})(function(e,t){return e.Validation=function(e){"use strict";var t={forceUpdate:!1,selector:"name",labelFormatter:"sentenceCase",valid:Function.prototype,invalid:Function.prototype},n={formatLabel:function(e,n){return u[t.labelFormatter](e,n)},format:function(){var e=Array.prototype.slice.call(arguments),t=e.shift();return t.replace(/\{(\d+)\}/g,function(t,n){return typeof e[n]!="undefined"?e[n]:t})}},r=function(){var r=function(t){return e.reduce(e.keys(t.validation||{}),function(e,t){return e[t]=void 0,e},{})},s=function(t,n){var r=t.validation?t.validation[n]||{}:{};if(e.isFunction(r)||e.isString(r))r={fn:r};return e.isArray(r)||(r=[r]),e.reduce(r,function(t,n){return e.each(e.without(e.keys(n),"msg"),function(e){t.push({fn:a[e],val:n[e],msg:n.msg})}),t},[])},o=function(t,r,i,o){return e.reduce(s(t,r),function(s,u){var f=e.extend({},n,a),l=u.fn.call(f,i,r,u.val,t,o);return l===!1||s===!1?!1:l&&!s?u.msg||l:s},"")},u=function(t,n){var r,i,s={},u=!0,a=e.clone(n);for(i in n)r=o(t,i,n[i],a),r&&(s[i]=r,u=!1);return{invalidAttrs:s,isValid:u}},f=function(t,n){return{preValidate:function(t,n){return o(this,t,n,e.extend({},this.attributes))},isValid:function(t){if(e.isString(t))return!o(this,t,this.get(t),e.extend({},this.attributes));if(e.isArray(t)){for(var n=0;n<t.length;n++)if(o(this,t[n],this.get(t[n]),e.extend({},this.attributes)))return!1;return!0}return t===!0&&this.validate(),this.validation?this._isValid:!0},validate:function(i,s){var o=this,a=!i,f=e.extend({},n,s),l=r(o),c=e.extend({},l,o.attributes,i),h=i||c,p=u(o,c);o._isValid=p.isValid,e.each(e.keys(l),function(e){var n=p.invalidAttrs.hasOwnProperty(e);n||f.valid(t,e,f.selector)}),e.each(e.keys(l),function(e){var n=p.invalidAttrs.hasOwnProperty(e),r=h.hasOwnProperty(e);n&&(r||a)&&f.invalid(t,e,p.invalidAttrs[e],f.selector)}),e.defer(function(){o.trigger("validated",o._isValid,o,p.invalidAttrs),o.trigger("validated:"+(o._isValid?"valid":"invalid"),o,p.invalidAttrs)});if(!f.forceUpdate&&e.intersection(e.keys(p.invalidAttrs),e.keys(h)).length>0)return p.invalidAttrs}}},l=function(t,n,r){e.extend(n,f(t,r))},c=function(e){delete e.validate,delete e.preValidate,delete e.isValid},h=function(e){l(this.view,e,this.options)},p=function(e){c(e)};return{version:"0.6.4",configure:function(n){e.extend(t,n)},bind:function(n,r){var s=n.model,o=n.collection;r=e.extend({},t,i,r);if(typeof s=="undefined"&&typeof o=="undefined")throw"Before you execute the binding your view must have a model or a collection.\nSee http://thedersen.com/projects/backbone-validation/#using-form-model-validation for more information.";s&&l(n,s,r),o&&(o.each(function(e){l(n,e,r)}),o.bind("add",h,{view:n,options:r}),o.bind("remove",p))},unbind:function(e){var t=e.model,n=e.collection;t&&c(e.model),n&&(n.each(function(e){c(e)}),n.unbind("add",h),n.unbind("remove",p))},mixin:f(null,t)}}(),i=r.callbacks={valid:function(e,t,n){e.$("["+n+"~="+t+"]").removeClass("invalid").removeAttr("data-error")},invalid:function(e,t,n,r){e.$("["+r+"~="+t+"]").addClass("invalid").attr("data-error",n)}},s=r.patterns={digits:/^\d+$/,number:/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/,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,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},o=r.messages={required:"{0} is required",acceptance:"{0} must be accepted",min:"{0} must be greater than or equal to {1}",max:"{0} must be less than or equal to {1}",range:"{0} must be between {1} and {2}",length:"{0} must be {1} characters",minLength:"{0} must be at least {1} characters",maxLength:"{0} must be at most {1} characters",rangeLength:"{0} must be between {1} and {2} characters",oneOf:"{0} must be one of: {1}",equalTo:"{0} must be the same as {1}",pattern:"{0} must be a valid {1}"},u=r.labelFormatters={none:function(e){return e},sentenceCase:function(e){return e.replace(/(?:^\w|[A-Z]|\b\w)/g,function(e,t){return t===0?e.toUpperCase():" "+e.toLowerCase()}).replace("_"," ")},label:function(e,t){return t.labels&&t.labels[e]||u.sentenceCase(e,t)}},a=r.validators=function(){var t=String.prototype.trim?function(e){return e===null?"":String.prototype.trim.call(e)}:function(e){var t=/^\s+/,n=/\s+$/;return e===null?"":e.toString().replace(t,"").replace(n,"")},n=function(t){return e.isNumber(t)||e.isString(t)&&t.match(s.number)},r=function(n){return!(e.isNull(n)||e.isUndefined(n)||e.isString(n)&&t(n)==="")};return{fn:function(t,n,r,i,s){return e.isString(r)&&(r=i[r]),r.call(i,t,n,s)},required:function(t,n,i,s,u){var a=e.isFunction(i)?i.call(s,t,n,u):i;if(!a&&!r(t))return!1;if(a&&!r(t))return this.format(o.required,this.formatLabel(n,s))},acceptance:function(t,n,r,i){if(t!=="true"&&(!e.isBoolean(t)||t===!1))return this.format(o.acceptance,this.formatLabel(n,i))},min:function(e,t,r,i){if(!n(e)||e<r)return this.format(o.min,this.formatLabel(t,i),r)},max:function(e,t,r,i){if(!n(e)||e>r)return this.format(o.max,this.formatLabel(t,i),r)},range:function(e,t,r,i){if(!n(e)||e<r[0]||e>r[1])return this.format(o.range,this.formatLabel(t,i),r[0],r[1])},length:function(e,n,i,s){if(!r(e)||t(e).length!==i)return this.format(o.length,this.formatLabel(n,s),i)},minLength:function(e,n,i,s){if(!r(e)||t(e).length<i)return this.format(o.minLength,this.formatLabel(n,s),i)},maxLength:function(e,n,i,s){if(!r(e)||t(e).length>i)return this.format(o.maxLength,this.formatLabel(n,s),i)},rangeLength:function(e,n,i,s){if(!r(e)||t(e).length<i[0]||t(e).length>i[1])return this.format(o.rangeLength,this.formatLabel(n,s),i[0],i[1])},oneOf:function(t,n,r,i){if(!e.include(r,t))return this.format(o.oneOf,this.formatLabel(n,i),r.join(", "))},equalTo:function(e,t,n,r,i){if(e!==i[n])return this.format(o.equalTo,this.formatLabel(t,r),this.formatLabel(n,r))},pattern:function(e,t,n,i){if(!r(e)||!e.toString().match(s[n]||n))return this.format(o.pattern,this.formatLabel(t,i),n)}}}();return r}(t),e.Validation}); |
@@ -1,2 +0,2 @@ | ||
// Backbone.Validation v0.6.3 | ||
// Backbone.Validation v0.6.4 | ||
// | ||
@@ -32,2 +32,23 @@ // Copyright (c) 2011-2012 Thomas Pedersen | ||
// Helper functions used when formatting error messages | ||
// ---------------------------------------------------- | ||
var formatFunctions = { | ||
// Uses the configured label formatter to format the attribute name | ||
// to make it more readable for the user | ||
formatLabel: function(attrName, model) { | ||
return defaultLabelFormatters[defaultOptions.labelFormatter](attrName, model); | ||
}, | ||
// Replaces nummeric placeholders like {0} in a string with arguments | ||
// passed to the function | ||
format: function() { | ||
var args = Array.prototype.slice.call(arguments); | ||
var text = args.shift(); | ||
return text.replace(/\{(\d+)\}/g, function(match, number) { | ||
return typeof args[number] !== 'undefined' ? args[number] : match; | ||
}); | ||
} | ||
}; | ||
// Validation | ||
@@ -90,3 +111,7 @@ // ---------- | ||
return _.reduce(getValidators(model, attr), function(memo, validator){ | ||
var result = validator.fn.call(defaultValidators, value, attr, validator.val, model, computed); | ||
// Pass the format functions plus the default | ||
// validators as the context to the validator | ||
var ctx = _.extend({}, formatFunctions, defaultValidators), | ||
result = validator.fn.call(ctx, value, attr, validator.val, model, computed); | ||
if(result === false || memo === false) { | ||
@@ -236,3 +261,3 @@ return false; | ||
// Current version of the library | ||
version: '0.6.3', | ||
version: '0.6.4', | ||
@@ -399,2 +424,3 @@ // Called to configure the default options | ||
// Built in validators | ||
@@ -416,18 +442,2 @@ // ------------------- | ||
// Uses the configured label formatter to format the attribute name | ||
// to make it more readable for the user | ||
var formatLabel = function(attrName, model) { | ||
return defaultLabelFormatters[defaultOptions.labelFormatter](attrName, model); | ||
}; | ||
// Replaces nummeric placeholders like {0} in a string with arguments | ||
// passed to the function | ||
var format = function() { | ||
var args = Array.prototype.slice.call(arguments); | ||
var text = args.shift(); | ||
return text.replace(/\{(\d+)\}/g, function(match, number) { | ||
return typeof args[number] !== 'undefined' ? args[number] : match; | ||
}); | ||
}; | ||
// Determines whether or not a value is a number | ||
@@ -461,3 +471,3 @@ var isNumber = function(value){ | ||
if (isRequired && !hasValue(value)) { | ||
return format(defaultMessages.required, formatLabel(attr, model)); | ||
return this.format(defaultMessages.required, this.formatLabel(attr, model)); | ||
} | ||
@@ -471,3 +481,3 @@ }, | ||
if(value !== 'true' && (!_.isBoolean(value) || value === false)) { | ||
return format(defaultMessages.acceptance, formatLabel(attr, model)); | ||
return this.format(defaultMessages.acceptance, this.formatLabel(attr, model)); | ||
} | ||
@@ -481,3 +491,3 @@ }, | ||
if (!isNumber(value) || value < minValue) { | ||
return format(defaultMessages.min, formatLabel(attr, model), minValue); | ||
return this.format(defaultMessages.min, this.formatLabel(attr, model), minValue); | ||
} | ||
@@ -491,3 +501,3 @@ }, | ||
if (!isNumber(value) || value > maxValue) { | ||
return format(defaultMessages.max, formatLabel(attr, model), maxValue); | ||
return this.format(defaultMessages.max, this.formatLabel(attr, model), maxValue); | ||
} | ||
@@ -501,3 +511,3 @@ }, | ||
if(!isNumber(value) || value < range[0] || value > range[1]) { | ||
return format(defaultMessages.range, formatLabel(attr, model), range[0], range[1]); | ||
return this.format(defaultMessages.range, this.formatLabel(attr, model), range[0], range[1]); | ||
} | ||
@@ -511,3 +521,3 @@ }, | ||
if (!hasValue(value) || trim(value).length !== length) { | ||
return format(defaultMessages.length, formatLabel(attr, model), length); | ||
return this.format(defaultMessages.length, this.formatLabel(attr, model), length); | ||
} | ||
@@ -521,3 +531,3 @@ }, | ||
if (!hasValue(value) || trim(value).length < minLength) { | ||
return format(defaultMessages.minLength, formatLabel(attr, model), minLength); | ||
return this.format(defaultMessages.minLength, this.formatLabel(attr, model), minLength); | ||
} | ||
@@ -531,3 +541,3 @@ }, | ||
if (!hasValue(value) || trim(value).length > maxLength) { | ||
return format(defaultMessages.maxLength, formatLabel(attr, model), maxLength); | ||
return this.format(defaultMessages.maxLength, this.formatLabel(attr, model), maxLength); | ||
} | ||
@@ -541,3 +551,3 @@ }, | ||
if(!hasValue(value) || trim(value).length < range[0] || trim(value).length > range[1]) { | ||
return format(defaultMessages.rangeLength, formatLabel(attr, model), range[0], range[1]); | ||
return this.format(defaultMessages.rangeLength, this.formatLabel(attr, model), range[0], range[1]); | ||
} | ||
@@ -551,3 +561,3 @@ }, | ||
if(!_.include(values, value)){ | ||
return format(defaultMessages.oneOf, formatLabel(attr, model), values.join(', ')); | ||
return this.format(defaultMessages.oneOf, this.formatLabel(attr, model), values.join(', ')); | ||
} | ||
@@ -561,3 +571,3 @@ }, | ||
if(value !== computed[equalTo]) { | ||
return format(defaultMessages.equalTo, formatLabel(attr, model), formatLabel(equalTo, model)); | ||
return this.format(defaultMessages.equalTo, this.formatLabel(attr, model), this.formatLabel(equalTo, model)); | ||
} | ||
@@ -571,3 +581,3 @@ }, | ||
if (!hasValue(value) || !value.toString().match(defaultPatterns[pattern] || pattern)) { | ||
return format(defaultMessages.pattern, formatLabel(attr, model), pattern); | ||
return this.format(defaultMessages.pattern, this.formatLabel(attr, model), pattern); | ||
} | ||
@@ -574,0 +584,0 @@ } |
@@ -1,2 +0,2 @@ | ||
// Backbone.Validation v0.6.3 | ||
// Backbone.Validation v0.6.4 | ||
// | ||
@@ -8,2 +8,2 @@ // Copyright (c) 2011-2012 Thomas Pedersen | ||
// http://thedersen.com/projects/backbone-validation | ||
Backbone.Validation=function(e){"use strict";var t={forceUpdate:!1,selector:"name",labelFormatter:"sentenceCase",valid:Function.prototype,invalid:Function.prototype},n=function(){var n=function(t){return e.reduce(e.keys(t.validation||{}),function(e,t){return e[t]=void 0,e},{})},i=function(t,n){var r=t.validation?t.validation[n]||{}:{};if(e.isFunction(r)||e.isString(r))r={fn:r};return e.isArray(r)||(r=[r]),e.reduce(r,function(t,n){return e.each(e.without(e.keys(n),"msg"),function(e){t.push({fn:u[e],val:n[e],msg:n.msg})}),t},[])},s=function(t,n,r,s){return e.reduce(i(t,n),function(e,i){var o=i.fn.call(u,r,n,i.val,t,s);return o===!1||e===!1?!1:o&&!e?i.msg||o:e},"")},o=function(t,n){var r,i,o={},u=!0,a=e.clone(n);for(i in n)r=s(t,i,n[i],a),r&&(o[i]=r,u=!1);return{invalidAttrs:o,isValid:u}},a=function(t,r){return{preValidate:function(t,n){return s(this,t,n,e.extend({},this.attributes))},isValid:function(t){if(e.isString(t))return!s(this,t,this.get(t),e.extend({},this.attributes));if(e.isArray(t)){for(var n=0;n<t.length;n++)if(s(this,t[n],this.get(t[n]),e.extend({},this.attributes)))return!1;return!0}return t===!0&&this.validate(),this.validation?this._isValid:!0},validate:function(i,s){var u=this,a=!i,f=e.extend({},r,s),l=n(u),c=e.extend({},l,u.attributes,i),h=i||c,p=o(u,c);u._isValid=p.isValid,e.each(e.keys(l),function(e){var n=p.invalidAttrs.hasOwnProperty(e);n||f.valid(t,e,f.selector)}),e.each(e.keys(l),function(e){var n=p.invalidAttrs.hasOwnProperty(e),r=h.hasOwnProperty(e);n&&(r||a)&&f.invalid(t,e,p.invalidAttrs[e],f.selector)}),e.defer(function(){u.trigger("validated",u._isValid,u,p.invalidAttrs),u.trigger("validated:"+(u._isValid?"valid":"invalid"),u,p.invalidAttrs)});if(!f.forceUpdate&&e.intersection(e.keys(p.invalidAttrs),e.keys(h)).length>0)return p.invalidAttrs}}},f=function(t,n,r){e.extend(n,a(t,r))},l=function(e){delete e.validate,delete e.preValidate,delete e.isValid},c=function(e){f(this.view,e,this.options)},h=function(e){l(e)};return{version:"0.6.3",configure:function(n){e.extend(t,n)},bind:function(n,i){var s=n.model,o=n.collection;i=e.extend({},t,r,i);if(typeof s=="undefined"&&typeof o=="undefined")throw"Before you execute the binding your view must have a model or a collection.\nSee http://thedersen.com/projects/backbone-validation/#using-form-model-validation for more information.";s&&f(n,s,i),o&&(o.each(function(e){f(n,e,i)}),o.bind("add",c,{view:n,options:i}),o.bind("remove",h))},unbind:function(e){var t=e.model,n=e.collection;t&&l(e.model),n&&(n.each(function(e){l(e)}),n.unbind("add",c),n.unbind("remove",h))},mixin:a(null,t)}}(),r=n.callbacks={valid:function(e,t,n){e.$("["+n+"~="+t+"]").removeClass("invalid").removeAttr("data-error")},invalid:function(e,t,n,r){e.$("["+r+"~="+t+"]").addClass("invalid").attr("data-error",n)}},i=n.patterns={digits:/^\d+$/,number:/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/,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,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},s=n.messages={required:"{0} is required",acceptance:"{0} must be accepted",min:"{0} must be greater than or equal to {1}",max:"{0} must be less than or equal to {1}",range:"{0} must be between {1} and {2}",length:"{0} must be {1} characters",minLength:"{0} must be at least {1} characters",maxLength:"{0} must be at most {1} characters",rangeLength:"{0} must be between {1} and {2} characters",oneOf:"{0} must be one of: {1}",equalTo:"{0} must be the same as {1}",pattern:"{0} must be a valid {1}"},o=n.labelFormatters={none:function(e){return e},sentenceCase:function(e){return e.replace(/(?:^\w|[A-Z]|\b\w)/g,function(e,t){return t===0?e.toUpperCase():" "+e.toLowerCase()}).replace("_"," ")},label:function(e,t){return t.labels&&t.labels[e]||o.sentenceCase(e,t)}},u=n.validators=function(){var n=String.prototype.trim?function(e){return e===null?"":String.prototype.trim.call(e)}:function(e){var t=/^\s+/,n=/\s+$/;return e===null?"":e.toString().replace(t,"").replace(n,"")},r=function(e,n){return o[t.labelFormatter](e,n)},u=function(){var e=Array.prototype.slice.call(arguments),t=e.shift();return t.replace(/\{(\d+)\}/g,function(t,n){return typeof e[n]!="undefined"?e[n]:t})},a=function(t){return e.isNumber(t)||e.isString(t)&&t.match(i.number)},f=function(t){return!(e.isNull(t)||e.isUndefined(t)||e.isString(t)&&n(t)==="")};return{fn:function(t,n,r,i,s){return e.isString(r)&&(r=i[r]),r.call(i,t,n,s)},required:function(t,n,i,o,a){var l=e.isFunction(i)?i.call(o,t,n,a):i;if(!l&&!f(t))return!1;if(l&&!f(t))return u(s.required,r(n,o))},acceptance:function(t,n,i,o){if(t!=="true"&&(!e.isBoolean(t)||t===!1))return u(s.acceptance,r(n,o))},min:function(e,t,n,i){if(!a(e)||e<n)return u(s.min,r(t,i),n)},max:function(e,t,n,i){if(!a(e)||e>n)return u(s.max,r(t,i),n)},range:function(e,t,n,i){if(!a(e)||e<n[0]||e>n[1])return u(s.range,r(t,i),n[0],n[1])},length:function(e,t,i,o){if(!f(e)||n(e).length!==i)return u(s.length,r(t,o),i)},minLength:function(e,t,i,o){if(!f(e)||n(e).length<i)return u(s.minLength,r(t,o),i)},maxLength:function(e,t,i,o){if(!f(e)||n(e).length>i)return u(s.maxLength,r(t,o),i)},rangeLength:function(e,t,i,o){if(!f(e)||n(e).length<i[0]||n(e).length>i[1])return u(s.rangeLength,r(t,o),i[0],i[1])},oneOf:function(t,n,i,o){if(!e.include(i,t))return u(s.oneOf,r(n,o),i.join(", "))},equalTo:function(e,t,n,i,o){if(e!==o[n])return u(s.equalTo,r(t,i),r(n,i))},pattern:function(e,t,n,o){if(!f(e)||!e.toString().match(i[n]||n))return u(s.pattern,r(t,o),n)}}}();return n}(_); | ||
Backbone.Validation=function(e){"use strict";var t={forceUpdate:!1,selector:"name",labelFormatter:"sentenceCase",valid:Function.prototype,invalid:Function.prototype},n={formatLabel:function(e,n){return u[t.labelFormatter](e,n)},format:function(){var e=Array.prototype.slice.call(arguments),t=e.shift();return t.replace(/\{(\d+)\}/g,function(t,n){return typeof e[n]!="undefined"?e[n]:t})}},r=function(){var r=function(t){return e.reduce(e.keys(t.validation||{}),function(e,t){return e[t]=void 0,e},{})},s=function(t,n){var r=t.validation?t.validation[n]||{}:{};if(e.isFunction(r)||e.isString(r))r={fn:r};return e.isArray(r)||(r=[r]),e.reduce(r,function(t,n){return e.each(e.without(e.keys(n),"msg"),function(e){t.push({fn:a[e],val:n[e],msg:n.msg})}),t},[])},o=function(t,r,i,o){return e.reduce(s(t,r),function(s,u){var f=e.extend({},n,a),l=u.fn.call(f,i,r,u.val,t,o);return l===!1||s===!1?!1:l&&!s?u.msg||l:s},"")},u=function(t,n){var r,i,s={},u=!0,a=e.clone(n);for(i in n)r=o(t,i,n[i],a),r&&(s[i]=r,u=!1);return{invalidAttrs:s,isValid:u}},f=function(t,n){return{preValidate:function(t,n){return o(this,t,n,e.extend({},this.attributes))},isValid:function(t){if(e.isString(t))return!o(this,t,this.get(t),e.extend({},this.attributes));if(e.isArray(t)){for(var n=0;n<t.length;n++)if(o(this,t[n],this.get(t[n]),e.extend({},this.attributes)))return!1;return!0}return t===!0&&this.validate(),this.validation?this._isValid:!0},validate:function(i,s){var o=this,a=!i,f=e.extend({},n,s),l=r(o),c=e.extend({},l,o.attributes,i),h=i||c,p=u(o,c);o._isValid=p.isValid,e.each(e.keys(l),function(e){var n=p.invalidAttrs.hasOwnProperty(e);n||f.valid(t,e,f.selector)}),e.each(e.keys(l),function(e){var n=p.invalidAttrs.hasOwnProperty(e),r=h.hasOwnProperty(e);n&&(r||a)&&f.invalid(t,e,p.invalidAttrs[e],f.selector)}),e.defer(function(){o.trigger("validated",o._isValid,o,p.invalidAttrs),o.trigger("validated:"+(o._isValid?"valid":"invalid"),o,p.invalidAttrs)});if(!f.forceUpdate&&e.intersection(e.keys(p.invalidAttrs),e.keys(h)).length>0)return p.invalidAttrs}}},l=function(t,n,r){e.extend(n,f(t,r))},c=function(e){delete e.validate,delete e.preValidate,delete e.isValid},h=function(e){l(this.view,e,this.options)},p=function(e){c(e)};return{version:"0.6.4",configure:function(n){e.extend(t,n)},bind:function(n,r){var s=n.model,o=n.collection;r=e.extend({},t,i,r);if(typeof s=="undefined"&&typeof o=="undefined")throw"Before you execute the binding your view must have a model or a collection.\nSee http://thedersen.com/projects/backbone-validation/#using-form-model-validation for more information.";s&&l(n,s,r),o&&(o.each(function(e){l(n,e,r)}),o.bind("add",h,{view:n,options:r}),o.bind("remove",p))},unbind:function(e){var t=e.model,n=e.collection;t&&c(e.model),n&&(n.each(function(e){c(e)}),n.unbind("add",h),n.unbind("remove",p))},mixin:f(null,t)}}(),i=r.callbacks={valid:function(e,t,n){e.$("["+n+"~="+t+"]").removeClass("invalid").removeAttr("data-error")},invalid:function(e,t,n,r){e.$("["+r+"~="+t+"]").addClass("invalid").attr("data-error",n)}},s=r.patterns={digits:/^\d+$/,number:/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/,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,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},o=r.messages={required:"{0} is required",acceptance:"{0} must be accepted",min:"{0} must be greater than or equal to {1}",max:"{0} must be less than or equal to {1}",range:"{0} must be between {1} and {2}",length:"{0} must be {1} characters",minLength:"{0} must be at least {1} characters",maxLength:"{0} must be at most {1} characters",rangeLength:"{0} must be between {1} and {2} characters",oneOf:"{0} must be one of: {1}",equalTo:"{0} must be the same as {1}",pattern:"{0} must be a valid {1}"},u=r.labelFormatters={none:function(e){return e},sentenceCase:function(e){return e.replace(/(?:^\w|[A-Z]|\b\w)/g,function(e,t){return t===0?e.toUpperCase():" "+e.toLowerCase()}).replace("_"," ")},label:function(e,t){return t.labels&&t.labels[e]||u.sentenceCase(e,t)}},a=r.validators=function(){var t=String.prototype.trim?function(e){return e===null?"":String.prototype.trim.call(e)}:function(e){var t=/^\s+/,n=/\s+$/;return e===null?"":e.toString().replace(t,"").replace(n,"")},n=function(t){return e.isNumber(t)||e.isString(t)&&t.match(s.number)},r=function(n){return!(e.isNull(n)||e.isUndefined(n)||e.isString(n)&&t(n)==="")};return{fn:function(t,n,r,i,s){return e.isString(r)&&(r=i[r]),r.call(i,t,n,s)},required:function(t,n,i,s,u){var a=e.isFunction(i)?i.call(s,t,n,u):i;if(!a&&!r(t))return!1;if(a&&!r(t))return this.format(o.required,this.formatLabel(n,s))},acceptance:function(t,n,r,i){if(t!=="true"&&(!e.isBoolean(t)||t===!1))return this.format(o.acceptance,this.formatLabel(n,i))},min:function(e,t,r,i){if(!n(e)||e<r)return this.format(o.min,this.formatLabel(t,i),r)},max:function(e,t,r,i){if(!n(e)||e>r)return this.format(o.max,this.formatLabel(t,i),r)},range:function(e,t,r,i){if(!n(e)||e<r[0]||e>r[1])return this.format(o.range,this.formatLabel(t,i),r[0],r[1])},length:function(e,n,i,s){if(!r(e)||t(e).length!==i)return this.format(o.length,this.formatLabel(n,s),i)},minLength:function(e,n,i,s){if(!r(e)||t(e).length<i)return this.format(o.minLength,this.formatLabel(n,s),i)},maxLength:function(e,n,i,s){if(!r(e)||t(e).length>i)return this.format(o.maxLength,this.formatLabel(n,s),i)},rangeLength:function(e,n,i,s){if(!r(e)||t(e).length<i[0]||t(e).length>i[1])return this.format(o.rangeLength,this.formatLabel(n,s),i[0],i[1])},oneOf:function(t,n,r,i){if(!e.include(r,t))return this.format(o.oneOf,this.formatLabel(n,i),r.join(", "))},equalTo:function(e,t,n,r,i){if(e!==i[n])return this.format(o.equalTo,this.formatLabel(t,r),this.formatLabel(n,r))},pattern:function(e,t,n,i){if(!r(e)||!e.toString().match(s[n]||n))return this.format(o.pattern,this.formatLabel(t,i),n)}}}();return r}(_); |
@@ -1,2 +0,2 @@ | ||
// Backbone.Validation v0.6.3 | ||
// Backbone.Validation v0.6.4 | ||
// | ||
@@ -24,2 +24,23 @@ // Copyright (c) 2011-2012 Thomas Pedersen | ||
// Helper functions used when formatting error messages | ||
// ---------------------------------------------------- | ||
var formatFunctions = { | ||
// Uses the configured label formatter to format the attribute name | ||
// to make it more readable for the user | ||
formatLabel: function(attrName, model) { | ||
return defaultLabelFormatters[defaultOptions.labelFormatter](attrName, model); | ||
}, | ||
// Replaces nummeric placeholders like {0} in a string with arguments | ||
// passed to the function | ||
format: function() { | ||
var args = Array.prototype.slice.call(arguments); | ||
var text = args.shift(); | ||
return text.replace(/\{(\d+)\}/g, function(match, number) { | ||
return typeof args[number] !== 'undefined' ? args[number] : match; | ||
}); | ||
} | ||
}; | ||
// Validation | ||
@@ -82,3 +103,7 @@ // ---------- | ||
return _.reduce(getValidators(model, attr), function(memo, validator){ | ||
var result = validator.fn.call(defaultValidators, value, attr, validator.val, model, computed); | ||
// Pass the format functions plus the default | ||
// validators as the context to the validator | ||
var ctx = _.extend({}, formatFunctions, defaultValidators), | ||
result = validator.fn.call(ctx, value, attr, validator.val, model, computed); | ||
if(result === false || memo === false) { | ||
@@ -228,3 +253,3 @@ return false; | ||
// Current version of the library | ||
version: '0.6.3', | ||
version: '0.6.4', | ||
@@ -391,2 +416,3 @@ // Called to configure the default options | ||
// Built in validators | ||
@@ -408,18 +434,2 @@ // ------------------- | ||
// Uses the configured label formatter to format the attribute name | ||
// to make it more readable for the user | ||
var formatLabel = function(attrName, model) { | ||
return defaultLabelFormatters[defaultOptions.labelFormatter](attrName, model); | ||
}; | ||
// Replaces nummeric placeholders like {0} in a string with arguments | ||
// passed to the function | ||
var format = function() { | ||
var args = Array.prototype.slice.call(arguments); | ||
var text = args.shift(); | ||
return text.replace(/\{(\d+)\}/g, function(match, number) { | ||
return typeof args[number] !== 'undefined' ? args[number] : match; | ||
}); | ||
}; | ||
// Determines whether or not a value is a number | ||
@@ -453,3 +463,3 @@ var isNumber = function(value){ | ||
if (isRequired && !hasValue(value)) { | ||
return format(defaultMessages.required, formatLabel(attr, model)); | ||
return this.format(defaultMessages.required, this.formatLabel(attr, model)); | ||
} | ||
@@ -463,3 +473,3 @@ }, | ||
if(value !== 'true' && (!_.isBoolean(value) || value === false)) { | ||
return format(defaultMessages.acceptance, formatLabel(attr, model)); | ||
return this.format(defaultMessages.acceptance, this.formatLabel(attr, model)); | ||
} | ||
@@ -473,3 +483,3 @@ }, | ||
if (!isNumber(value) || value < minValue) { | ||
return format(defaultMessages.min, formatLabel(attr, model), minValue); | ||
return this.format(defaultMessages.min, this.formatLabel(attr, model), minValue); | ||
} | ||
@@ -483,3 +493,3 @@ }, | ||
if (!isNumber(value) || value > maxValue) { | ||
return format(defaultMessages.max, formatLabel(attr, model), maxValue); | ||
return this.format(defaultMessages.max, this.formatLabel(attr, model), maxValue); | ||
} | ||
@@ -493,3 +503,3 @@ }, | ||
if(!isNumber(value) || value < range[0] || value > range[1]) { | ||
return format(defaultMessages.range, formatLabel(attr, model), range[0], range[1]); | ||
return this.format(defaultMessages.range, this.formatLabel(attr, model), range[0], range[1]); | ||
} | ||
@@ -503,3 +513,3 @@ }, | ||
if (!hasValue(value) || trim(value).length !== length) { | ||
return format(defaultMessages.length, formatLabel(attr, model), length); | ||
return this.format(defaultMessages.length, this.formatLabel(attr, model), length); | ||
} | ||
@@ -513,3 +523,3 @@ }, | ||
if (!hasValue(value) || trim(value).length < minLength) { | ||
return format(defaultMessages.minLength, formatLabel(attr, model), minLength); | ||
return this.format(defaultMessages.minLength, this.formatLabel(attr, model), minLength); | ||
} | ||
@@ -523,3 +533,3 @@ }, | ||
if (!hasValue(value) || trim(value).length > maxLength) { | ||
return format(defaultMessages.maxLength, formatLabel(attr, model), maxLength); | ||
return this.format(defaultMessages.maxLength, this.formatLabel(attr, model), maxLength); | ||
} | ||
@@ -533,3 +543,3 @@ }, | ||
if(!hasValue(value) || trim(value).length < range[0] || trim(value).length > range[1]) { | ||
return format(defaultMessages.rangeLength, formatLabel(attr, model), range[0], range[1]); | ||
return this.format(defaultMessages.rangeLength, this.formatLabel(attr, model), range[0], range[1]); | ||
} | ||
@@ -543,3 +553,3 @@ }, | ||
if(!_.include(values, value)){ | ||
return format(defaultMessages.oneOf, formatLabel(attr, model), values.join(', ')); | ||
return this.format(defaultMessages.oneOf, this.formatLabel(attr, model), values.join(', ')); | ||
} | ||
@@ -553,3 +563,3 @@ }, | ||
if(value !== computed[equalTo]) { | ||
return format(defaultMessages.equalTo, formatLabel(attr, model), formatLabel(equalTo, model)); | ||
return this.format(defaultMessages.equalTo, this.formatLabel(attr, model), this.formatLabel(equalTo, model)); | ||
} | ||
@@ -563,3 +573,3 @@ }, | ||
if (!hasValue(value) || !value.toString().match(defaultPatterns[pattern] || pattern)) { | ||
return format(defaultMessages.pattern, formatLabel(attr, model), pattern); | ||
return this.format(defaultMessages.pattern, this.formatLabel(attr, model), pattern); | ||
} | ||
@@ -566,0 +576,0 @@ } |
{ | ||
"name": "backbone-validation", | ||
"title": "Backbone.Validation", | ||
"version": "0.6.3", | ||
"version": "0.6.4", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Thomas Pedersen", |
@@ -1,2 +0,2 @@ | ||
# Backbone.Validation v0.6.3 | ||
# Backbone.Validation v0.6.4 | ||
@@ -771,2 +771,6 @@ A validation plugin for [Backbone.js](http://documentcloud.github.com/backbone) that validates both your model as well as form input. | ||
#### v0.6.4 | ||
* `format(...)` and `formatLabel(...)` are made available for custom validators on `this` (Thanks to [rafanoronha](https://github.com/rafanoronha)) | ||
#### v0.6.3 | ||
@@ -773,0 +777,0 @@ |
@@ -16,2 +16,23 @@ Backbone.Validation = (function(_){ | ||
// Helper functions used when formatting error messages | ||
// ---------------------------------------------------- | ||
var formatFunctions = { | ||
// Uses the configured label formatter to format the attribute name | ||
// to make it more readable for the user | ||
formatLabel: function(attrName, model) { | ||
return defaultLabelFormatters[defaultOptions.labelFormatter](attrName, model); | ||
}, | ||
// Replaces nummeric placeholders like {0} in a string with arguments | ||
// passed to the function | ||
format: function() { | ||
var args = Array.prototype.slice.call(arguments); | ||
var text = args.shift(); | ||
return text.replace(/\{(\d+)\}/g, function(match, number) { | ||
return typeof args[number] !== 'undefined' ? args[number] : match; | ||
}); | ||
} | ||
}; | ||
// Validation | ||
@@ -74,3 +95,7 @@ // ---------- | ||
return _.reduce(getValidators(model, attr), function(memo, validator){ | ||
var result = validator.fn.call(defaultValidators, value, attr, validator.val, model, computed); | ||
// Pass the format functions plus the default | ||
// validators as the context to the validator | ||
var ctx = _.extend({}, formatFunctions, defaultValidators), | ||
result = validator.fn.call(ctx, value, attr, validator.val, model, computed); | ||
if(result === false || memo === false) { | ||
@@ -220,3 +245,3 @@ return false; | ||
// Current version of the library | ||
version: '0.6.3', | ||
version: '0.6.4', | ||
@@ -383,2 +408,3 @@ // Called to configure the default options | ||
// Built in validators | ||
@@ -400,18 +426,2 @@ // ------------------- | ||
// Uses the configured label formatter to format the attribute name | ||
// to make it more readable for the user | ||
var formatLabel = function(attrName, model) { | ||
return defaultLabelFormatters[defaultOptions.labelFormatter](attrName, model); | ||
}; | ||
// Replaces nummeric placeholders like {0} in a string with arguments | ||
// passed to the function | ||
var format = function() { | ||
var args = Array.prototype.slice.call(arguments); | ||
var text = args.shift(); | ||
return text.replace(/\{(\d+)\}/g, function(match, number) { | ||
return typeof args[number] !== 'undefined' ? args[number] : match; | ||
}); | ||
}; | ||
// Determines whether or not a value is a number | ||
@@ -445,3 +455,3 @@ var isNumber = function(value){ | ||
if (isRequired && !hasValue(value)) { | ||
return format(defaultMessages.required, formatLabel(attr, model)); | ||
return this.format(defaultMessages.required, this.formatLabel(attr, model)); | ||
} | ||
@@ -455,3 +465,3 @@ }, | ||
if(value !== 'true' && (!_.isBoolean(value) || value === false)) { | ||
return format(defaultMessages.acceptance, formatLabel(attr, model)); | ||
return this.format(defaultMessages.acceptance, this.formatLabel(attr, model)); | ||
} | ||
@@ -465,3 +475,3 @@ }, | ||
if (!isNumber(value) || value < minValue) { | ||
return format(defaultMessages.min, formatLabel(attr, model), minValue); | ||
return this.format(defaultMessages.min, this.formatLabel(attr, model), minValue); | ||
} | ||
@@ -475,3 +485,3 @@ }, | ||
if (!isNumber(value) || value > maxValue) { | ||
return format(defaultMessages.max, formatLabel(attr, model), maxValue); | ||
return this.format(defaultMessages.max, this.formatLabel(attr, model), maxValue); | ||
} | ||
@@ -485,3 +495,3 @@ }, | ||
if(!isNumber(value) || value < range[0] || value > range[1]) { | ||
return format(defaultMessages.range, formatLabel(attr, model), range[0], range[1]); | ||
return this.format(defaultMessages.range, this.formatLabel(attr, model), range[0], range[1]); | ||
} | ||
@@ -495,3 +505,3 @@ }, | ||
if (!hasValue(value) || trim(value).length !== length) { | ||
return format(defaultMessages.length, formatLabel(attr, model), length); | ||
return this.format(defaultMessages.length, this.formatLabel(attr, model), length); | ||
} | ||
@@ -505,3 +515,3 @@ }, | ||
if (!hasValue(value) || trim(value).length < minLength) { | ||
return format(defaultMessages.minLength, formatLabel(attr, model), minLength); | ||
return this.format(defaultMessages.minLength, this.formatLabel(attr, model), minLength); | ||
} | ||
@@ -515,3 +525,3 @@ }, | ||
if (!hasValue(value) || trim(value).length > maxLength) { | ||
return format(defaultMessages.maxLength, formatLabel(attr, model), maxLength); | ||
return this.format(defaultMessages.maxLength, this.formatLabel(attr, model), maxLength); | ||
} | ||
@@ -525,3 +535,3 @@ }, | ||
if(!hasValue(value) || trim(value).length < range[0] || trim(value).length > range[1]) { | ||
return format(defaultMessages.rangeLength, formatLabel(attr, model), range[0], range[1]); | ||
return this.format(defaultMessages.rangeLength, this.formatLabel(attr, model), range[0], range[1]); | ||
} | ||
@@ -535,3 +545,3 @@ }, | ||
if(!_.include(values, value)){ | ||
return format(defaultMessages.oneOf, formatLabel(attr, model), values.join(', ')); | ||
return this.format(defaultMessages.oneOf, this.formatLabel(attr, model), values.join(', ')); | ||
} | ||
@@ -545,3 +555,3 @@ }, | ||
if(value !== computed[equalTo]) { | ||
return format(defaultMessages.equalTo, formatLabel(attr, model), formatLabel(equalTo, model)); | ||
return this.format(defaultMessages.equalTo, this.formatLabel(attr, model), this.formatLabel(equalTo, model)); | ||
} | ||
@@ -555,3 +565,3 @@ }, | ||
if (!hasValue(value) || !value.toString().match(defaultPatterns[pattern] || pattern)) { | ||
return format(defaultMessages.pattern, formatLabel(attr, model), pattern); | ||
return this.format(defaultMessages.pattern, this.formatLabel(attr, model), pattern); | ||
} | ||
@@ -558,0 +568,0 @@ } |
@@ -34,7 +34,2 @@ buster.testCase('Extending Backbone.Validation with custom validator', { | ||
})); | ||
}, | ||
"context is the validators object": function() { | ||
this.model.set({age:1}); | ||
assert.same(Backbone.Validation.validators, this.context); | ||
} | ||
@@ -122,1 +117,30 @@ }); | ||
}); | ||
buster.testCase("Formatting custom validator messages", { | ||
setUp: function() { | ||
_.extend(Backbone.Validation.validators, { | ||
custom: function(value, attr, customValue, model) { | ||
if (value !== customValue) { | ||
return this.format("{0} must be equal to {1}", this.formatLabel(attr, model), customValue); | ||
} | ||
} | ||
}); | ||
var Model = Backbone.Model.extend({ | ||
validation: { | ||
name: { | ||
custom: 'custom' | ||
} | ||
} | ||
}); | ||
this.model = new Model(); | ||
Backbone.Validation.bind(new Backbone.View({ | ||
model: this.model | ||
})); | ||
}, | ||
"a custom validator can return a formatted message": function() { | ||
assert.equals({name: 'Name must be equal to custom'}, this.model.validate({name:''})); | ||
} | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
612070
15681
897