HEX
Server: Apache
System: Linux s198.coreserver.jp 5.15.0-151-generic #161-Ubuntu SMP Tue Jul 22 14:25:40 UTC 2025 x86_64
User: nagasaki (10062)
PHP: 7.1.33
Disabled: NONE
Upload Files
File: //usr/local/rvm/gems/ruby-3.0.2/doc/activemodel-6.1.4.1/ri/ActiveModel/Validator/cdesc-Validator.ri
U:RDoc::NormalClass[iI"Validator:ETI"ActiveModel::Validator;TI"Object;To:RDoc::Markup::Document:@parts[o;;[S:RDoc::Markup::Heading:
leveli:	textI"Active \Model \Validator;To:RDoc::Markup::BlankLineo:RDoc::Markup::Paragraph;[I"5A simple base class that can be used along with ;TI":ActiveModel::Validations::ClassMethods.validates_with;T@o:RDoc::Markup::Verbatim;[I"class Person
;TI"(  include ActiveModel::Validations
;TI""  validates_with MyValidator
;TI"	end
;TI"
;TI"0class MyValidator < ActiveModel::Validator
;TI"  def validate(record)
;TI"    if some_complex_logic
;TI">      record.errors.add(:base, "This record is invalid")
;TI"
    end
;TI"  end
;TI"
;TI"  private
;TI"     def some_complex_logic
;TI"      # ...
;TI"
    end
;TI"	end
;T:@format0o;
;[I"QAny class that inherits from ActiveModel::Validator must implement a method ;TI"0called +validate+ which accepts a +record+.;T@o;;[I"class Person
;TI"(  include ActiveModel::Validations
;TI""  validates_with MyValidator
;TI"	end
;TI"
;TI"0class MyValidator < ActiveModel::Validator
;TI"  def validate(record)
;TI"9    record # => The person instance being validated
;TI"H    options # => Any non-standard options passed to validates_with
;TI"  end
;TI"	end
;T;0o;
;[I"QTo cause a validation error, you must add to the +record+'s errors directly ;TI"(from within the validators message.;T@o;;[I"0class MyValidator < ActiveModel::Validator
;TI"  def validate(record)
;TI"F    record.errors.add :base, "This is some custom error message"
;TI"J    record.errors.add :first_name, "This is some complex validation"
;TI"    # etc...
;TI"  end
;TI"	end
;T;0o;
;[I"KTo add behavior to the initialize method, use the following signature:;T@o;;[I"0class MyValidator < ActiveModel::Validator
;TI"  def initialize(options)
;TI"    super
;TI"@    @my_custom_field = options[:field_name] || :first_name
;TI"  end
;TI"	end
;T;0o;
;[I"PNote that the validator is initialized only once for the whole application ;TI"0life cycle, and not on each validation run.;T@o;
;[I"SThe easiest way to add custom validators for validating individual attributes ;TI"@is with the convenient <tt>ActiveModel::EachValidator</tt>.;T@o;;[
I"7class TitleValidator < ActiveModel::EachValidator
;TI"3  def validate_each(record, attribute, value)
;TI"j    record.errors.add attribute, 'must be Mr., Mrs., or Dr.' unless %w(Mr. Mrs. Dr.).include?(value)
;TI"  end
;TI"	end
;T;0o;
;[I"EThis can now be used in combination with the +validates+ method ;TI"V(see <tt>ActiveModel::Validations::ClassMethods.validates</tt> for more on this).;T@o;;[I"class Person
;TI"(  include ActiveModel::Validations
;TI"  attr_accessor :title
;TI"
;TI"5  validates :title, presence: true, title: true
;TI"	end
;T;0o;
;[I"iIt can be useful to access the class that is using that validator when there are prerequisites such ;TI"tas an +attr_accessor+ being present. This class is accessible via <tt>options[:class]</tt> in the constructor. ;TI"7To set up your validator override the constructor.;T@o;;[I"0class MyValidator < ActiveModel::Validator
;TI""  def initialize(options={})
;TI"    super
;TI"9    options[:class].attr_accessor :custom_attribute
;TI"  end
;TI"end;T;0:
@fileI""lib/active_model/validator.rb;T:0@omit_headings_from_table_of_contents_below0;0;0[[
I"options;TI"R;T:publicFI""lib/active_model/validator.rb;T[[[[I"
class;T[[;[[I"	kind;T@~[I"new;T@~[:protected[[:private[[I"
instance;T[[;[[I"	kind;T@~[I"
validate;T@~[;[[;[[[U:RDoc::Context::Section[i0o;;[;0;0[@yI"ActiveModel;TcRDoc::NormalModule