Replies: 1 comment 2 replies
-
A similar case is with |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have come across a lot of times in my projects where a field under validation is required if another field has a specific value, and the same field under validation is prohibited unless that condition is met.
For example I have an eshop and I want to adjust a product's stock with a specific adjustment reason.
The adjustment reason might be something like
1 => Damaged
,2 => Theft or loss
,3 => Promotion
,99 => Other
.If the reason code is
99
I expect the user to provide the other adjustment reason title, otherwise the user should be prohibited from providing a custom reason title.Currently, alongside with custom rule, another possible way to achieve this is to use a combination of required_if and prohibited_unless. This is a part of my rules:
The above will yield one of the following errors based on the
adjustment_reason
This is totally ok and is working. However I have a lot of such situations and I end up repeating myself all over the place which makes the project harder to maintain.
So, my solution to this is a new validation rule
exclusive_*
:The whole set:
exclusive_if
,exclusive_unless
,exclusive_with
,exclusive_without
,Rule::exclusiveIf()
.I would love to make a PR on this but don't know if it is something worth working on. Any thoughts?
Beta Was this translation helpful? Give feedback.
All reactions