Add URL validation class.

This commit is contained in:
Andrew Tomaka 2012-05-03 01:25:26 -04:00
parent fae61bce2e
commit 56f2b7348e
2 changed files with 16 additions and 0 deletions

View file

@ -3,3 +3,6 @@ require File.expand_path('../application', __FILE__)
# Initialize the rails application # Initialize the rails application
WwwWhoisandrewCom::Application.initialize! WwwWhoisandrewCom::Application.initialize!
# Include the URL validator
require 'url_validator'

13
lib/url_validator.rb Normal file
View file

@ -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