Add URL validation class.
This commit is contained in:
parent
fae61bce2e
commit
56f2b7348e
2 changed files with 16 additions and 0 deletions
|
@ -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
13
lib/url_validator.rb
Normal 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
|
Loading…
Reference in a new issue