Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Validating a number greater than zero is failing

Tags:

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

like image 833
MrCujo Avatar asked Sep 19 '18 03:09

MrCujo


People also ask

What happens when a validator fails?

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.

How does laravel validate country code?

Usage. Validate a 2 character country code. use LVR\CountryCode\Two; $request->validate([ 'country' => ['required', new Two], ]);


2 Answers

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.)

like image 112
Tharindu Thisarasinghe Avatar answered Sep 29 '22 14:09

Tharindu Thisarasinghe


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

like image 33
effy Avatar answered Sep 29 '22 14:09

effy