Laravel: Validating a number greater than zero is failing

You can try this way ,

Before invoking the Validator::make() function, modify the set of rules by appending the value to compare to like so:

use Illuminate\Support\Facades\Validator;

Validator::extend('greater_than', function ($attribute, $value, $otherValue) {
      return intval($value) > intval($otherValue[0]);
});

$validation = Validator::make($input, ['amount' => 'required|numeric|greater_than:0']);

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