Skip to content

Instantly share code, notes, and snippets.

@marugoshi
Created February 9, 2012 06:30
Show Gist options
  • Save marugoshi/1777850 to your computer and use it in GitHub Desktop.
Save marugoshi/1777850 to your computer and use it in GitHub Desktop.
Custom url validation for Rails3
# -*- coding: utf-8 -*-
class UrlValidator < ::ActiveModel::EachValidator
def validate_each(record, attribute, value)
begin
uri = ::Addressable::URI.parse(value)
unless ["http", "https", "ftp"].include?(uri.scheme)
raise ::Addressable::URI::InvalidURIError
end
rescue ::Addressable::URI::InvalidURIError
record.errors[attribute] << "Invalid URL"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment