Skip to content

Commit 40255e4

Browse files
authored
Merge pull request rails#46397 from shouichi/document-validation-context
Document ActiveModel#validation_context [skip ci]
2 parents 3f8e31a + 8be20b0 commit 40255e4

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

activemodel/lib/active_model/validations.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ module Validations
4545
extend HelperMethods
4646
include HelperMethods
4747

48+
##
49+
# :method: validation_context
50+
# Returns the context when running validations.
51+
#
52+
# This is useful when running validations except a certain context (opposite to the +on+ option).
53+
#
54+
# class Person
55+
# include ActiveModel::Validations
56+
#
57+
# attr_accessor :name
58+
# validates :name, presence: true, if: -> { validation_context != :custom }
59+
# end
60+
#
61+
# person = Person.new
62+
# person.valid? #=> false
63+
# person.valid?(:new) #=> false
64+
# person.valid?(:custom) #=> true
65+
66+
##
4867
attr_accessor :validation_context
4968
private :validation_context=
5069
define_callbacks :validate, scope: :name

0 commit comments

Comments
 (0)