Created
February 9, 2012 06:30
-
-
Save marugoshi/1777850 to your computer and use it in GitHub Desktop.
Custom url validation for Rails3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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