-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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] Add unfilled_if & unfilled_with validation rules to Validator #34425
Conversation
72c22eb
to
86babcf
Compare
@GrahamCampbell - Here's a PR targeting 8.x, i'll shoot the one for laravel/laravel over too. On second thought, I'm considering if I should implement an "Unfilled*" rule a bit differently to make it more flexible and keep the feature similar to existing rules. Specifically I realized this type of rule seems more similar to the |
Make the unfilled_if match required_if, and implement unfilled_with method similar to original goal.
I'm not sure this is really necessary. If you know you want to ignore a given field's input on a given condition, just ignore it. No need to validate it. |
@taylorotwell - Quick question, am I wrong for thinking of the Validator feature as an "HTTP Request" validator? Your reply seems to indicate that I should think of the validator as a "Field Validator" and simply process the fields myself. However I always thought the documentation read as though this was an HTTP Request/Query validator. Given that the docs say: "validate incoming HTTP requests" I would assume my original interpretation is correct. So while "just ignore it" is one option, I would argue that being able to reject a request for including incompatible flags is a valid use case. Thoughts? Also, after a quick look, it seems the gits of this feature would be similar to Symfony's |
I've wanted this on a few occasions as well and have previously extended the validator with a For example, imagine a conditional field The existing I don't mind implementing this as a custom rule, however it gets a bit nasty because it must re-implement the logic from the following protected Validator methods:
|
Hey @jessarcher - assuming the logic of Agreed on it being pretty nasty - in all honestly I just directly duplicated those methods in my package to get it working. One of many reasons it'd be preferable to have the rule as a stock part of laravel's validator. Again, i'm going to update the rule name since I like your idea so much better, but the current itteration of the package is here: https://github.com/mallardduck/extended-validator-laravel |
Hey @mallardduck, thanks for the package tip! Glad to help with the naming too 😁 I wonder whether a PR to make Edit: PR here |
Great work @jessarcher - once a tagged release of For now there's a new 2.0.0 tag of my package with the improved rule names, so once the feature is released i'll increment from the 2.X tag. |
New Rules
unfilled_if
: A rule to refuse a field's input when another field is a given value. Complements:required_if
&filled
.unfilled_with
: A to refuse a fields's input when other fields are passed. Complements:required_with
&filled
.Adding these rules can help add more utility to validation to disallow field input when other specific field values, or fields, are passed.
Example
Rule usages:
Note: This is a super simplified example, instead of
nullable
you'd probably want to userequired_without_all:width,height
or something similar IRL depending on use-case.URL
Examples Request URL:
http://myapp.test/avatar?name=Danno+Boone&size=96&width=64
Error
PR also adds tests to cover new validation rules, and I have a translation string prepared to send to the main laravel repo.