New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to specify multiple contexts when defining a validation. #13754
Conversation
This was requested in #9659, but I think it was closed by mistake. |
@@ -67,7 +67,7 @@ module ClassMethods | |||
# | |||
# Options: | |||
# * <tt>:on</tt> - Specifies the context where this validation is active |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I think that the documentation of the #validate
method should be updated as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
|
|
||
topic = Topic.new | ||
assert topic.invalid?(:context2), "Validation does run on context 2" | ||
assert topic.errors[:base].include?(ERROR_MESSAGE) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's also add the case where the validation does not run.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added another test case. Thanks for the feedback!
@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! |
@robin850, will do. At this point I feel the patch is complete. Is there anything else required in order to have the PR accepted? |
@vpuzzella : Thanks! Everything seems fine to me but I'm not a merger, this decision is up to @senny. |
@vpuzzella the PR does no longer apply and needs a rebase. |
Example: validates_presence_of :name, on: [:update, :custom_validation_context]
@senny, rebased and CI build passing. |
Ability to specify multiple contexts when defining a validation.
Thanks. |
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] It would be much simpler if you could write it as: validates :name, presence: true, on: [:save, :step1] In fact, consider the how callbacks are specified (before_save, before_create, before_update) this actually seems like a bug. |
@vpuzzella Any comments on my suggestion above? |
@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! :-) |
Example:
validates_presence_of :name, on: [:update, :custom_validation_context]