Created
June 25, 2012 04:27
-
-
Save bluemont/2986523 to your computer and use it in GitHub Desktop.
ActiveModel URL Validator
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
class UrlValidator < ActiveModel::EachValidator | |
def validate_each(record, attribute, value) | |
valid = begin | |
URI.parse(value).kind_of?(URI::HTTP) | |
rescue URI::InvalidURIError | |
false | |
end | |
unless valid | |
record.errors[attribute] << (options[:message] || "is an invalid URL") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see https://gist.github.com/adamico/2a781d6a4fc2ff577e7f for a Rails 4 with I18n messages and updated and simplified specs