-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
wrong :value in validation rules 'gt', 'gte', 'lt', 'lte' for type numeric #52036
Comments
After further testing your issue, it appears that gt:0 is not treating 0 as a value but as a key of the array. For instance, if you add three items in the first row and two items in the second row, gt:0 will return items greater than 3, and gt:1 will return items greater than 2. Similarly, if you add more array items, gt:3 will behave the same if the array has more than three rows. Here is an example of the data: $data = [ |
Is it how it should work? I am not at the laptop right now but if I remember correctly, if we replace gt:0 with gt:10 then an actual number is used so I still think that the behaviour is inconsistent, no? |
@prateekgoyal261 No it doesn't treat is as a key it should behave accordingly size rule. Though this is interesting. The first picture that came to my mind was similar to @azgooon's, but I knew that the gt, gte, etc. rules behave according to the size rule. The size rule varies depending on the type. For an array, size corresponds to the count of the array. That rule has been applied to your amount field, treating it as an array type but in your case it should be treated as integer which is quite odd. Make sure you pass the amount as integer perhaps you may have passed it as an array. |
@Lakshan-Madushanka as per example, the amount is definitely passed as int. |
So this is quite the gotcha but @prateekgoyal261 is correct. It's because you're using the wildcard as the root of the array causing the value of $data = [
"values" => [
[
"amount" => 10,
"currency" => "GBP"
],
[
"amount" => 20,
"currency" => "GBP"
]
]
];
$rules = [
"values.*.amount" => ["required", "numeric", "gt:0"],
"values.*.currency" => ["array"]
];
$validator = Validator::make($data, $rules);
dd($validator->getMessageBag()->toArray()); |
Thanks @driesvints, that makes sense. |
Laravel Version
10.48.15
PHP Version
8.3.4
Database Driver & Version
No response
Description
I came across what I think is the bug and would appreciate it if someone could have a second opinion.
In the "steps to reproduce" I have included a snippet which when executed I would have expected to return 2 errors both referencing the
currency
field, stating that thecurrency
field must be an array (this is just to demonstration purposes). Instead, there are 4 errors 2 about the array and 2 about theamount
field informing that theamount
field should be greater than 2.am I missing something or is it not desirable behavior?
Thanks,
Tom.
Steps To Reproduce
The text was updated successfully, but these errors were encountered: