A 'greater than zero' validation rule gives the wrong result in Laravel
gt:0 or min:1 gives results that do not match the number that was typedShort answer
Laravel's gt, gte and min rules compare by the type of the value: string length for text, the number itself only for numeric data. A form sends text, so add the numeric or integer rule next to gt:0. Use gt:0 for any positive number, min:1 only for whole numbers, and nullable if blank is allowed.
Fix it fast
1Find out which cause you have
Read the rule list for the field and check whether numeric or integer is in it.
grep -rn "gt:\|min:" app/Http2Match what you see, then jump to its fix
What does the error look like?
gt:0 or min:1 gives results that do not match the number that was typedmin:1 rejects 0.5 or 0.25Leaving the optional field empty returns a validation error
Cause 1 of 3
No numeric rule, so the value is compared as text
1Check
Laravel's size rule says string data is measured in characters, and numeric data is measured by value only when the field also has the numeric or integer rule. gt, gte and min follow the same convention.
grep -rn "'gt:0'\|gt:0\|min:1" app/Http2Fix
Add numeric (or integer for whole numbers) to the rule list.
$request->validate([
'price' => ['required', 'numeric', 'gt:0'],
'qty' => ['required', 'integer', 'min:1'],
]);3Verify
Send 0, -1 and 2.5 and confirm only the valid ones pass.
php artisan tinker --execute='foreach ([0, -1, "2.5"] as $v) { var_dump(Validator::make(["p" => $v], ["p" => "numeric|gt:0"])->passes()); }'Confirmed by laravel.com
Cause 2 of 3
min:1 used where fractions are allowed
1Check
min:1 means a minimum value of 1. A price or weight of 0.5 is greater than zero but below that minimum.
grep -rn "min:1" app/Http2Fix
Use gt:0 when any positive number is fine, and keep min:1 for counts that must be whole.
'weight' => ['required', 'numeric', 'gt:0'],3Verify
Submit 0.5 and expect a pass, then 0 and expect a failure.
php artisan tinker --execute='var_dump(Validator::make(["w" => "0.5"], ["w" => "numeric|gt:0"])->passes());'Confirmed by laravel.com
Cause 3 of 3
An empty field becomes null and fails the rules
1Check
Laravel's TrimStrings and ConvertEmptyStringsToNull middleware turn blank input into null. The docs say optional fields must be marked nullable, or null is treated as invalid.
php artisan route:list -v --path=products2Fix
If the field is optional, add nullable before the other rules. If it is required, leave required and show the message to the user.
'discount' => ['nullable', 'numeric', 'gt:0'],3Verify
Submit the form with the field blank, then with 0, and confirm the two results differ.
php artisan tinker --execute='var_dump(Validator::make(["d" => null], ["d" => "nullable|numeric|gt:0"])->passes());'Confirmed by laravel.com
How do I stop it coming back?
- Write numeric or integer into every rule list that compares size, even when the HTML input is type=number.
- Add a test for the boundary values 0, 1 and a fraction for every numeric field.
- Decide per field: required, nullable or sometimes, and write it first in the list.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Composer says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack end
composer require maatwebsite/excel fails to install
Composer says the package needs an older PHP than the one installed
Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.
3 causes, each with a fixBack end
composer install says the lock file is not compatible and asks for update
Your lock file does not contain a compatible set of packages. Please run composer update.
composer install checked the locked packages against this machine's PHP and extensions and found a conflict.
3 causes, each with a fixBack end
composer require laravel/horizon fails: ext-pcntl is missing
laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system
Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.
3 causes, each with a fixBack end
composer require laravel/passport stops with 'requirements could not be resolved'
... but these were not loaded, likely because it conflicts with another require.
Composer found no Passport release that fits your project at once.
4 causes, each with a fixBack end
Composer says the root composer.json requires a PHP version your PHP does not satisfy
Your requirements could not be resolved to an installable set of packages.
Composer compares the php constraint in your composer.json, or in a package, with the PHP that runs Composer.
3 causes, each with a fix