n-time/backbone.validations — Issue #15: Optional fields
Author: roblevintennis
Created: 2012-01-24 17:15:03+00:00
State: open

I have a comments form with optional site field. So url is a nice validation but my field is optional. I tried required:false type:url but it didn't work. The following custom validation worked but I believe there should be a general way to have type validations only kick in if the field was supplied for an optional field:

optionalUrl: function(attributeName, attributeValue) {
var urlRegex = /* same url regex you have ;) */
if(attributeValue.length) {
if (_.isString(attributeValue) && !attributeValue.match(urlRegex)) {
return "Malformed URL";
}
}
}

BTW, thanks for the plugin! Saved me tons of coding!