Skip to content
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

Merged
merged 1 commit into from Jan 27, 2014

Conversation

vpuzzella
Copy link
Contributor

Example:

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

@vpuzzella
Copy link
Contributor Author

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
Copy link
Member

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.

?

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@rbarazi
Copy link

rbarazi commented Jan 19, 2014

👍


topic = Topic.new
assert topic.invalid?(:context2), "Validation does run on context 2"
assert topic.errors[:base].include?(ERROR_MESSAGE)
Copy link
Member

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.

Copy link
Contributor Author

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!

@robin850
Copy link
Member

@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
Copy link
Contributor Author

@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
Copy link
Member

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

@senny
Copy link
Member

senny commented Jan 27, 2014

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

Example:

validates_presence_of :name, on: [:update, :custom_validation_context]
@vpuzzella
Copy link
Contributor Author

@senny, rebased and CI build passing.

carlosantoniodasilva added a commit that referenced this pull request Jan 27, 2014
Ability to specify multiple contexts when defining a validation.
@carlosantoniodasilva carlosantoniodasilva merged commit 50d9717 into rails:master Jan 27, 2014
@carlosantoniodasilva
Copy link
Member

Thanks.

@pierre-pretorius
Copy link

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
Copy link

@vpuzzella Any comments on my suggestion above?

@robin850
Copy link
Member

@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! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants