diff --git a/config/environment.rb b/config/environment.rb index b58e7e1..b6522d0 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -3,3 +3,6 @@ require File.expand_path('../application', __FILE__) # Initialize the rails application WwwWhoisandrewCom::Application.initialize! + +# Include the URL validator +require 'url_validator' \ No newline at end of file diff --git a/lib/url_validator.rb b/lib/url_validator.rb new file mode 100644 index 0000000..61c48ef --- /dev/null +++ b/lib/url_validator.rb @@ -0,0 +1,13 @@ +class UrlValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + begin + uri = URI.parse(value) + resp = uri.kind_of?(URI::HTTP) + rescue URI::InvalidURIError + resp = false + end + unless resp == true + record.errors[attribute] << (options[:message] || "is not an url") + end + end +end \ No newline at end of file