backbone.validations
backbone.validations copied to clipboard
Optional fields
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!
I would like to second this request. Any followup on this?