Class: UrlValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/url_validator.rb

Overview

UrlValidator

Custom validator for URLs.

By default, only URLs for the HTTP(S) protocols will be considered valid. Provide a `:protocols` option to configure accepted protocols.

Example:

class User < ActiveRecord::Base
  validates :personal_url, url: true

  validates :ftp_url, url: { protocols: %w(ftp) }

  validates :git_url, url: { protocols: %w(http https ssh git) }
end

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object


19
20
21
22
23
# File 'app/validators/url_validator.rb', line 19

def validate_each(record, attribute, value)
  unless valid_url?(value)
    record.errors.add(attribute, "must be a valid URL")
  end
end