1

I want that my quantity[field name] must be zero(0) or positive

I define following rules in my model.

public function rules()
{
    return [
        [['quantity'], 'integer', 'min' => 0],
        [['quantity'], 'integer', 'max' => 1000000],

    ];
}

And it is working properly. And display error message when i try to enter negative value .

But The Problem is it accept -0 [ minus zero ]

How to restrict user to do not enter -0 ?

Thanks

Yasin Patel
  • 5,624
  • 8
  • 31
  • 53

2 Answers2

3

Well, nothing strange since -0 is equal to 0, you could simply use a match rule :

['quantity', 'match', 'pattern' => '/^[0-9]*$/'],
['quantity', 'integer', 'min' => 0, 'max' => 1000000],

Read more about match validator.

soju
  • 25,111
  • 3
  • 68
  • 70
0

Try this:

['quantity', 'compare', 'compareValue' => 0, 'operator' => '>='],

for more details, you can have a look at How to specify a validation rule in Yii2 which will be greater than or less than of a specific number or value?

Community
  • 1
  • 1
Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78