I gotta validate a price field which needs to be greater than zero (0.01 is valid) so I have the following validation:
$request->validate([ 'product_price' => 'required|numeric|gt:0', ]);
The problem is that when I enter a string in the 'product_price' field I'm getting an error:
InvalidArgumentException The values under comparison must be of the same type
why is that? I mean, I'm checking that it should be numeric before even checking that it's > 0
If validation fails during a traditional HTTP request, a redirect response to the previous URL will be generated. If the incoming request is an XHR request, a JSON response containing the validation error messages will be returned.
Usage. Validate a 2 character country code. use LVR\CountryCode\Two; $request->validate([ 'country' => ['required', new Two], ]);
gt
, gte
, lt
and lte
are added in Laravel 5.6 and later versions, I'm guessing that must be the reason for you get the error. (It's working for me though.)
I think you can try like this
$request->validate([ 'product_price' => 'required|numeric|min:0|not_in:0', ]);
min:0
make sure the minimum value is 0 and no negative values are allowed. not_in:0
make sure value cannot be 0. So, combination of both of these rules does the job.
You can define meaningful error messages for certain rule. (You can achieve the same result using regular expressions as well.)
I can see that none of the other answers have addressed the real reason you're getting this error, I'll try and provide some insights and a solution to it.
The problem here is that Laravel is testing all validation rules and not stopping after the first validation error which in your case is numeric
(which fails since the value provided is a string), if it did that the error with the gt
validator being provided a string value wouldn't be thrown since the validator has already exited after the numeric
error.
To have Laravel stop the validation checks after the first failed validation rule, you can prefix your validation rules with the bail
validator which basically tells Laravel to stop after the first error.
The resulting code would look like this:
$request->validate([ 'product_price' => 'bail|required|numeric|gt:0', ]);
Note that this solution also makes it so that only a single error is ever returned per field, if in your UI you usually display all the errors for a particular field at a time (instead of only picking the first one from the message bag), this solution would change that.
More information on the bail
validation rule can be found here: https://laravel.com/docs/6.x/validation#rule-bail
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With