Module: ActiveModel::Validations
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/active_model/validations.rb,
lib/active_model/validations/with.rb,
lib/active_model/validations/format.rb,
lib/active_model/validations/length.rb,
lib/active_model/validations/absence.rb,
lib/active_model/validations/presence.rb,
lib/active_model/validations/callbacks.rb,
lib/active_model/validations/clusivity.rb,
lib/active_model/validations/exclusion.rb,
lib/active_model/validations/inclusion.rb,
lib/active_model/validations/validates.rb,
lib/active_model/validations/acceptance.rb,
lib/active_model/validations/confirmation.rb,
lib/active_model/validations/numericality.rb
Overview
Active Model Length Validator
Defined Under Namespace
Modules: Callbacks, ClassMethods, Clusivity, HelperMethods Classes: AbsenceValidator, AcceptanceValidator, ConfirmationValidator, ExclusionValidator, FormatValidator, InclusionValidator, LengthValidator, NumericalityValidator, PresenceValidator, WithValidator
Instance Method Summary collapse
-
#errors ⇒ Object
Returns the
Errors
object that holds all information about attribute error messages. -
#initialize_dup(other) ⇒ Object
Clean the
Errors
object if instance is duped. -
#invalid?(context = nil) ⇒ Boolean
Performs the opposite of
valid?
. -
#valid?(context = nil) ⇒ Boolean
Runs all the specified validations and returns
true
if no errors were added otherwisefalse
. -
#validates_with(*args, &block) ⇒ Object
Passes the record off to the class or classes specified and allows them to add errors based on more complex conditions.
Instance Method Details
#errors ⇒ Object
Returns the Errors
object that holds all information about attribute error messages.
class Person
include ActiveModel::Validations
attr_accessor :name
validates_presence_of :name
end
person = Person.new
person.valid? # => false
person.errors # => #<ActiveModel::Errors:0x007fe603816640 @messages={name:["can't be blank"]}>
278 279 280 |
# File 'lib/active_model/validations.rb', line 278 def errors @errors ||= Errors.new(self) end |
#initialize_dup(other) ⇒ Object
Clean the Errors
object if instance is duped.
260 261 262 263 |
# File 'lib/active_model/validations.rb', line 260 def initialize_dup(other) #:nodoc: @errors = nil super end |
#invalid?(context = nil) ⇒ Boolean
Performs the opposite of valid?
. Returns true
if errors were added, false
otherwise.
class Person
include ActiveModel::Validations
attr_accessor :name
validates_presence_of :name
end
person = Person.new
person.name = ''
person.invalid? # => true
person.name = 'david'
person.invalid? # => false
Context can optionally be supplied to define which callbacks to test against (the context is defined on the validations using :on
).
class Person
include ActiveModel::Validations
attr_accessor :name
validates_presence_of :name, on: :new
end
person = Person.new
person.invalid? # => false
person.invalid?(:new) # => true
348 349 350 |
# File 'lib/active_model/validations.rb', line 348 def invalid?(context = nil) !valid?(context) end |
#valid?(context = nil) ⇒ Boolean
Runs all the specified validations and returns true
if no errors were added otherwise false
.
class Person
include ActiveModel::Validations
attr_accessor :name
validates_presence_of :name
end
person = Person.new
person.name = ''
person.valid? # => false
person.name = 'david'
person.valid? # => true
Context can optionally be supplied to define which callbacks to test against (the context is defined on the validations using :on
).
class Person
include ActiveModel::Validations
attr_accessor :name
validates_presence_of :name, on: :new
end
person = Person.new
person.valid? # => true
person.valid?(:new) # => false
311 312 313 314 315 316 317 |
# File 'lib/active_model/validations.rb', line 311 def valid?(context = nil) current_context, self.validation_context = validation_context, context errors.clear run_validations! ensure self.validation_context = current_context end |
#validates_with(*args, &block) ⇒ Object
Passes the record off to the class or classes specified and allows them to add errors based on more complex conditions.
class Person
include ActiveModel::Validations
validate :instance_validations
def instance_validations
validates_with MyValidator
end
end
Please consult the class method documentation for more information on creating your own validator.
You may also pass it multiple classes, like so:
class Person
include ActiveModel::Validations
validate :instance_validations, on: :create
def instance_validations
validates_with MyValidator, MyOtherValidator
end
end
Standard configuration options (:on
, :if
and :unless
), which are available on the class version of validates_with
, should instead be placed on the validates
method as these are applied and tested in the callback.
If you pass any additional configuration options, they will be passed to the class and available as options
, please refer to the class version of this method for more information.
139 140 141 142 143 144 145 |
# File 'lib/active_model/validations/with.rb', line 139 def validates_with(*args, &block) = args. args.each do |klass| validator = klass.new(, &block) validator.validate(self) end end |