module Validations
Ruby on Rails 7.1.6
Active Model Validations
Provides a full validation framework to your objects.
A minimal implementation could be:
class Person
include ActiveModel::Validations
attr_accessor :first_name, :last_name
validates_each :first_name, :last_name do |record, attr, value|
record.errors.add attr, "starts with z." if value.start_with?("z")
end
end
Which provides you with the full standard validation stack that you know from Active Record:
person = Person.new
person.valid? # => true
person.invalid? # => false
person.first_name = 'zoolander'
person.valid? # => false
person.invalid? # => true
person.errors.messages # => {first_name:["starts with z."]}
Note that ActiveModel::Validations automatically adds an errors method to your instances initialized with a new ActiveModel::Errors object, so there is no need for you to do this manually.
Namespace
Modules
- ActiveModel::Validations::Callbacks
- ActiveModel::Validations::ClassMethods
- ActiveModel::Validations::HelperMethods
Classes
Includes
Extends
Attributes
Methods (defined here)
- # errors
- # invalid?
- # valid?
- # validate
- # validate!
- # validates_with
- # validation_context
Private methods (3)
Show private methods Implementation detail — not part of the public API.
Used by
Included by (3)
Methods (inherited)
From ActiveModel::Callbacks
(1)
From ActiveModel::Naming
(7)
- # model_name
- self. param_key
- self. plural
- self. route_key
- self. singular
- self. singular_route_key
- self. uncountable?
From ActiveModel::Translation
(3)
From ActiveModel::Validations::HelperMethods
(11)
From ActiveSupport::Concern
(3)
- # class_methods
- # included
- # prepended
From ActiveSupport::Callbacks
(1)
From ActiveSupport::DescendantsTracker
(4)
- # descendants
- # subclasses
- self. descendants
- self. subclasses