backbone.validations icon indicating copy to clipboard operation
backbone.validations copied to clipboard

Optional fields

Open roblevintennis opened this issue 13 years ago • 1 comments

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!

roblevintennis avatar Jan 24 '12 17:01 roblevintennis

I would like to second this request. Any followup on this?

d1b1 avatar May 03 '12 21:05 d1b1