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

[8.x] Added prohibits validation #38612

Merged
merged 3 commits into from
Aug 31, 2021
Merged

Conversation

samlev
Copy link
Contributor

@samlev samlev commented Aug 31, 2021

NB: I originally submitted this as prohibited_with, which was turned down because with would behave differently from required_with. I feel that changing the wording to prohibits resolves this issue without compromising on the functionality of this validation.

I've encountered a requirement in a few systems where two potential fields could be passed, but if one exists the other cannot exist. The prohibited_if validation allows you to counter this only if you know the potential values of the second field, so I've added prohibits as a way to create a simple NAND between fields.

The most recent example was working with the Facebook Business SDK, where a customer record could accept email or emails, but passing both fields causes an error (because it can't be resolved into a correct single value). With this validation, I could catch that earlier in the application and ensure that I'm not having to deal with API/SDK errors. For this example:

Validator::validate([
    'email' => 'example@example.com', 
    'emails' => ['example@example.com', 'other.example@example.com']
], [
    'email' => 'prohibits:emails'
]);

You can also pass multiple fields for a situation where you have one field or multiple fields (e.g. passing a model ID as well as fields that can only be set on creation). In this case, if any of the listed prohibits fields exist, it will trigger a validation failure.

Validator::validate([
    'post_id' => 1, 
    'created_by' => 2,
    'created_at' => '2020-07-16 20:54:22'
], [
    'post_id' => 'prohibits:created_by,created_at'
]);

Finally, this can be used with required_without to create a XOR, where one property must be passed, but never both.

Validator::validate([
    'email' => 'example@example.com', 
    'emails' => ['example@example.com', 'other.example@example.com']
], [
    'email' => ['prohibits:emails', 'required_without:emails']
]);

If you choose to merge, I will send PRs to laravel/docs and laravel/laravel.

samlev and others added 2 commits August 31, 2021 20:42
@GrahamCampbell GrahamCampbell changed the title Added prohibits validation [8.x] Added prohibits validation Aug 31, 2021
@taylorotwell taylorotwell merged commit 4c18c8d into laravel:8.x Aug 31, 2021
victorvilella pushed a commit to cdsistemas/framework that referenced this pull request Oct 12, 2021
* Added `prohibits` validation

* Update src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Co-authored-by: Julius Kiekbusch <jubeki99@gmail.com>

* Style CI Fix

Co-authored-by: Julius Kiekbusch <jubeki99@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants