instance method
valid?
Ruby on Rails 6.0.6
Signature
valid?(context = nil)
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
Parameters
-
contextopt = nil
Source
# File activemodel/lib/active_model/validations.rb, line 334
def valid?(context = nil)
current_context, self.validation_context = validation_context, context
errors.clear
run_validations!
ensure
self.validation_context = current_context
end
Defined in activemodel/lib/active_model/validations.rb line 334
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveModel::Validations