class ActiveModel::Validator

end<br>end<br>options.attr_accessor :custom_attribute
super
def initialize(options={})
class MyValidator < ActiveModel::Validator
To set up your validator override the constructor.
as an attr_accessor being present. This class is accessible via options[:class] in the constructor.
It can be useful to access the class that is using that validator when there are prerequisites such
end
validates :title, presence: true, title: true
attr_accessor :title
include ActiveModel::Validations
class Person
(see ActiveModel::Validations::ClassMethods#validates for more on this).
This can now be used in combination with the validates method
end
end
record.errors.add attribute, ‘must be Mr., Mrs., or Dr.’ unless %w(Mr. Mrs. Dr.).include?(value)
def validate_each(record, attribute, value)
class TitleValidator < ActiveModel::EachValidator
is with the convenient ActiveModel::EachValidator.
The easiest way to add custom validators for validating individual attributes
life cycle, and not on each validation run.
Note that the validator is initialized only once for the whole application
end
end
@my_custom_field = options || :first_name
super
def initialize(options)
class MyValidator < ActiveModel::Validator
To add behavior to the initialize method, use the following signature:
end
end
# etc…
record.errors.add :first_name, “This is some complex validation”
record.errors.add :base, “This is some custom error message”
def validate(record)
class MyValidator < ActiveModel::Validator
from within the validators message.
To cause a validation error, you must add to the record‘s errors directly
end
end
options # => Any non-standard options passed to validates_with
record # => The person instance being validated
def validate(record)
class MyValidator < ActiveModel::Validator
end
validates_with MyValidator
include ActiveModel::Validations
class Person
called validate which accepts a record.
Any class that inherits from ActiveModel::Validator must implement a method
end
end
# …
def some_complex_logic
private
end
end
record.errors.add(:base, “This record is invalid”)
if some_complex_logic
def validate(record)
class MyValidator < ActiveModel::Validator
end
validates_with MyValidator
include ActiveModel::Validations
class Person
ActiveModel::Validations::ClassMethods.validates_with
A simple base class that can be used along with
== Active Model Validator

def self.kind

AcceptanceValidator.kind # => :acceptance
PresenceValidator.kind # => :presence

Returns the kind of the validator.
def self.kind
  @kind ||= name.split("::").last.underscore.chomp("_validator").to_sym unless anonymous?
end

def initialize(options = {})

Accepts options that will be made available through the +options+ reader.
def initialize(options = {})
  @options = options.except(:class).freeze
end

def kind

AcceptanceValidator.new(attributes: [:terms]).kind # => :acceptance
PresenceValidator.new(attributes: [:username]).kind # => :presence

Returns the kind for this validator.
def kind
  self.class.kind
end

def validate(record)

to the records +errors+ array where necessary.
Override this method in subclasses with validation logic, adding errors
def validate(record)
  raise NotImplementedError, "Subclasses must implement a validate(record) method."
end