Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

A 'greater than zero' validation rule gives the wrong result in Laravel

Errorgt:0 or min:1 gives results that do not match the number that was typed

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

  • Applies to Laravel 12, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/Http
  2. 2Match what you see, then jump to its fix

    1. 1The rule is gt:0 or min:1 alone and results look randomNo numeric rule, so the value is compared as textFix
    2. 2Values between 0 and 1, such as 0.5, are rejectedmin:1 used where fractions are allowedFix
    3. 3The error appears only when the box is left blankAn empty field becomes null and fails the rulesFix

What does the error look like?

  • gt:0 or min:1 gives results that do not match the number that was typed
  • min:1 rejects 0.5 or 0.25
  • Leaving 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/Http

2Fix

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

Did this fix it?

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/Http

2Fix

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

Did this fix it?

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=products

2Fix

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

Did this fix it?

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 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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.