Skip to content

Instantly share code, notes, and snippets.

@tonekk
Forked from bluemont/url_validator.rb
Last active August 29, 2015 14:14
Show Gist options
  • Save tonekk/975ee32ccc4e4190186b to your computer and use it in GitHub Desktop.
Save tonekk/975ee32ccc4e4190186b to your computer and use it in GitHub Desktop.
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