class ActiveModelValidators::UrlValidator
URL validator¶ ↑
Constants
- DEFAULT_PROTOCOLS
Default protocols used for validation if no custom protocols provided
DEFAULT_PROTOCOLS = [::URI::HTTP, ::URI::HTTPS]
Public Instance Methods
validate_each(record, attribute, value)
click to toggle source
Adds error if there is invalid URL address.
By default it works with http
and https
.
It can be easily extended with protocols param
-
record
- ActiveRecord model -
attr
- model attribute to store an URL -
value
- value, supposed to be a valid URL-adress
Example¶ ↑
class MyModel < ActiveRecord::Base # default usage validates :my_url, :'active_model_validators/url' => true end class MyModel < ActiveRecord::Base # with custom URL protocols validates :my_url, :'active_model_validators/url' => { protocols: [URI::HTTP, URI::HTTPS, URI::FTP] } end valid_url = 'https://www.valid.com' my_model_instance = MyModel.create(my_url: valid_url) my_model_instance.valid? # => true my_model_instance.my_attribute = 'invalid_url' my_model_instance.valid? # => false
# File lib/active_model_validators/url_validator.rb, line 38 def validate_each(record, attribute, value) record.errors.add(attribute, error_message) unless url_valid?(value) end