Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Ability to specify multiple contexts when defining a validation. #13754

Merged
merged 1 commit into from

6 participants

@vpuzzella

Example:

validates_presence_of :name, on: [:update, :custom_validation_context]

@vpuzzella

This was requested in #9659, but I think it was closed by mistake.

activemodel/lib/active_model/validations.rb
@@ -67,7 +67,7 @@ module ClassMethods
#
# Options:
# * <tt>:on</tt> - Specifies the context where this validation is active
@robin850 Collaborator

I think that context could be plural here now. Also, what do you think about :

Specifies the contexts where this validation is active. You can either pass a symbol or a collection of actions.

?

@robin850 Collaborator

Also, I think that the documentation of the #validate method should be updated as well.

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
activemodel/lib/active_model/validations.rb
@@ -67,7 +67,7 @@ module ClassMethods
#
# Options:
# * <tt>:on</tt> - Specifies the context where this validation is active
- # (e.g. <tt>on: :create</tt> or <tt>on: :custom_validation_context</tt>)
+ # (e.g. <tt>on: :create</tt> or <tt>on: :custom_validation_context or <tt>on: [:create, :custom_validation_context]</tt>)
@robin850 Collaborator

Could you wrap this around 80 chars please ?

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...el/test/cases/validations/validations_context_test.rb
((15 lines not shown))
end
+
@robin850 Collaborator

:scissors:

Done. Thanks for the feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...el/test/cases/validations/validations_context_test.rb
@@ -36,4 +36,15 @@ def validate(record)
assert topic.invalid?(:create), "Validation does run on create if 'on' is set to create"
assert topic.errors[:base].include?(ERROR_MESSAGE)
end
+
+ test "with a class that adds errors on multiple contexts and validating a new model" do
+ Topic.validates_with(ValidatorThatAddsErrors, on: [:context1, :context2])
+ topic = Topic.new
+ assert topic.invalid?(:context1), "Validation does run on context 1"
+ assert topic.errors[:base].include?(ERROR_MESSAGE)
+
+ topic = Topic.new
+ assert topic.invalid?(:context2), "Validation does run on context 2"
+ assert topic.errors[:base].include?(ERROR_MESSAGE)
@senny Owner
senny added a note

let's also add the case where the validation does not run.

Added another test case. Thanks for the feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@senny senny commented on the diff
activemodel/CHANGELOG.md
@@ -1,3 +1,11 @@
+* Ability to specify multiple contexts when defining a validation.
+
@senny Owner
senny added a note

Can you add Example: before the example code? See other entries for reference.

Example added to changelog.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@robin850
Collaborator

@vpuzzella : For future contributions, could you post a comment once your patch is updated please ? :-) GitHub does not create notifications when you push the commit. Thank you!

This looks good to me so far!

@vpuzzella

@robin850, will do. At this point I feel the patch is complete. Is there anything else required in order to have the PR accepted?

@robin850
Collaborator

@vpuzzella : Thanks! Everything seems fine to me but I'm not a merger, this decision is up to @senny.

@senny
Owner

@vpuzzella the PR does no longer apply and needs a rebase.

@vpuzzella vpuzzella Ability to specify multiple contexts when defining a validation.
Example:

validates_presence_of :name, on: [:update, :custom_validation_context]
8855163
@vpuzzella

@senny, rebased and CI build passing.

@carlosantoniodasilva carlosantoniodasilva merged commit 50d9717 into rails:master
@carlosantoniodasilva

Thanks.

@pierre-pretorius

This feature is very useful thank you. I have the following suggestion: when someone specifies a validation with context :save, it should be called for :create and :update.

For example, if you have two steps, the first step someone needs to enter his name, the second step his lastname, but it should only be able to save if name, surname and token is populated, you currently have to do it like this:

validates :name, presence: true, on: [:create, :update, :step1]
validates :lastname, presence: true, on: [:create, :update, :step2]
validates :token, presence: true, on: [:create, :update]

It would be much simpler if you could write it as:

validates :name, presence: true, on: [:save, :step1]
validates :lastname, presence: true, on: [:save, :step2]
validates :token, presence: true, on: :save

In fact, consider the how callbacks are specified (before_save, before_create, before_update) this actually seems like a bug.

@pierre-pretorius

@vpuzzella Any comments on my suggestion above?

@robin850
Collaborator

@pierre-pretorius : Please post feature requests to the rails-core mailing list or send a patch (unless I'm wrong, this seems straightforward here) ; thank you! :-)

@benja83 benja83 referenced this pull request from a commit in benja83/toyotakataboard-relational
@benja83 benja83