-
Notifications
You must be signed in to change notification settings - Fork 11.5k
[8.x] Add $validator->sometimesNested() to work with nested arrays #38385
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
Conversation
Checks here on Github fail with:
Tbh, not sure why. Input welcome. |
There are union type support merged recently. That maybe the case as for the error coming from pipe character. |
Think you are right. Seems checks started to fail globally with #38383. |
Hi, I merged #38398 into 8.x to get the test suite passing again. I've put your PR in draft. Please rebase your PR against 8.x to make the tests of this PR pass. After you've done that and tests pass, mark this PR as ready for review. Thanks. |
Done. |
I don't think I would want an entirely different method here. |
@taylorotwell would it be an option to add an additional parameter to the end of the current Otherwise I don't see a way to get this done in the same method without breaking change. Bit more feedback would be much appreciated. |
I had the same issue. Needed a lot of custom code to work around the For validation messages, this got closed without further thinking: #36587 For validation rules, basically I make a loop on the items to add a rule for each of them.
Validation error message translation/matching is handled via a custom subclass using the idea in the PR mentionned above (because basically when it fails, you get an error for Maybe in a couple of years some addition will be made to the framework when enough people report the same difficulty to work with items in validation rules/messages. |
I consider this feature as small but helpful 'quality of life' improvement with no backwards compatibility issues.
Problem
It is not possible to use
$validator->sometimes()
with nested arrays inwithValidator($validator)
of aFormRequest
class.Given an array/request like this (or even more nested):
The
value
attribute can be an url or an email, depending on the value of an other field.$validator->sometimes()
is not aware of the current item/index. Dot notation with an asterix is also not working. So it seems there is no flexible way to to apply specifc rules for nested arrays, to archieve this:Solution
I assume it does not make sense to touch the existing
sometimes()
method. Hence, I would propose to add the following method toIlluminate\Validation\Validator
:How it works: Basically the functionality is pretty much the same as in
$validator->sometimes()
. The main difference is, that we pass a 'parent' key name as a first parameter to the method, get the values and loop trough it. If the callback returns true we add the rules based on the parent/index/key combination.Usage
Developers would have a convinient way to access and flexible validate nested items in a
FormRequest
class like this: