[8.x] Added prohibits
validation
#38612
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
NB: I originally submitted this as
prohibited_with
, which was turned down becausewith
would behave differently fromrequired_with
. I feel that changing the wording toprohibits
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 addedprohibits
as a way to create a simpleNAND
between fields.The most recent example was working with the Facebook Business SDK, where a customer record could accept
email
oremails
, 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: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.Finally, this can be used with
required_without
to create aXOR
, where one property must be passed, but never both.If you choose to merge, I will send PRs to
laravel/docs
andlaravel/laravel
.