提交 ff83868b 编写于 作者: R Rafael Mendonça França

Merge pull request #21535 from dmitry/feature/validate-multiple-contexts

Validate multiple contexts on `valid?` and `invalid?` at once
* Validate multiple contexts on `valid?` and `invalid?` at once.
Example:
class Person
include ActiveModel::Validations
attr_reader :name, :title
validates_presence_of :name, on: :create
validates_presence_of :title, on: :update
end
person = Person.new
person.valid?([:create, :update]) # => true
person.errors.messages # => {:name=>["can't be blank"], :title=>["can't be blank"]}
*Dmitry Polushkin*
* Add case_sensitive option for confirmation validator in models. * Add case_sensitive option for confirmation validator in models.
*Akshat Sharma* *Akshat Sharma*
......
...@@ -162,7 +162,7 @@ def validate(*args, &block) ...@@ -162,7 +162,7 @@ def validate(*args, &block)
options = options.dup options = options.dup
options[:if] = Array(options[:if]) options[:if] = Array(options[:if])
options[:if].unshift ->(o) { options[:if].unshift ->(o) {
Array(options[:on]).include?(o.validation_context) !(Array(options[:on]) & Array(o.validation_context)).empty?
} }
end end
......
...@@ -8,6 +8,7 @@ def teardown ...@@ -8,6 +8,7 @@ def teardown
end end
ERROR_MESSAGE = "Validation error from validator" ERROR_MESSAGE = "Validation error from validator"
ANOTHER_ERROR_MESSAGE = "Another validation error from validator"
class ValidatorThatAddsErrors < ActiveModel::Validator class ValidatorThatAddsErrors < ActiveModel::Validator
def validate(record) def validate(record)
...@@ -15,6 +16,12 @@ def validate(record) ...@@ -15,6 +16,12 @@ def validate(record)
end end
end end
class AnotherValidatorThatAddsErrors < ActiveModel::Validator
def validate(record)
record.errors[:base] << ANOTHER_ERROR_MESSAGE
end
end
test "with a class that adds errors on create and validating a new model with no arguments" do test "with a class that adds errors on create and validating a new model with no arguments" do
Topic.validates_with(ValidatorThatAddsErrors, on: :create) Topic.validates_with(ValidatorThatAddsErrors, on: :create)
topic = Topic.new topic = Topic.new
...@@ -46,4 +53,16 @@ def validate(record) ...@@ -46,4 +53,16 @@ def validate(record)
assert topic.invalid?(:context2), "Validation did not run on context2 when 'on' is set to context1 and context2" assert topic.invalid?(:context2), "Validation did not run on context2 when 'on' is set to context1 and context2"
assert topic.errors[:base].include?(ERROR_MESSAGE) assert topic.errors[:base].include?(ERROR_MESSAGE)
end end
test "with a class that validating a model for a multiple contexts" do
Topic.validates_with(ValidatorThatAddsErrors, on: :context1)
Topic.validates_with(AnotherValidatorThatAddsErrors, on: :context2)
topic = Topic.new
assert topic.valid?, "Validation ran with no context given when 'on' is set to context1 and context2"
assert topic.invalid?([:context1, :context2]), "Validation did not run on context1 when 'on' is set to context1 and context2"
assert topic.errors[:base].include?(ERROR_MESSAGE)
assert topic.errors[:base].include?(ANOTHER_ERROR_MESSAGE)
end
end end
...@@ -60,7 +60,7 @@ def save!(options={}) ...@@ -60,7 +60,7 @@ def save!(options={})
# Validations with no <tt>:on</tt> option will run no matter the context. Validations with # Validations with no <tt>:on</tt> option will run no matter the context. Validations with
# some <tt>:on</tt> option will only run in the specified context. # some <tt>:on</tt> option will only run in the specified context.
def valid?(context = nil) def valid?(context = nil)
context ||= (new_record? ? :create : :update) context ||= default_validation_context
output = super(context) output = super(context)
errors.empty? && output errors.empty? && output
end end
...@@ -69,6 +69,10 @@ def valid?(context = nil) ...@@ -69,6 +69,10 @@ def valid?(context = nil)
protected protected
def default_validation_context
new_record? ? :create : :update
end
def raise_validation_error def raise_validation_error
raise(RecordInvalid.new(self)) raise(RecordInvalid.new(self))
end end
......
...@@ -52,6 +52,13 @@ def test_valid_using_special_context ...@@ -52,6 +52,13 @@ def test_valid_using_special_context
assert r.valid?(:special_case) assert r.valid?(:special_case)
end end
def test_invalid_using_multiple_contexts
r = WrongReply.new(:title => 'Wrong Create')
assert r.invalid?([:special_case, :create])
assert_equal "Invalid", r.errors[:author_name].join
assert_equal "is Wrong Create", r.errors[:title].join
end
def test_validate def test_validate
r = WrongReply.new r = WrongReply.new
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册