Yii2 中的验证

Validation in Yii2

我希望我的数量[字段名称]必须是零(0)或 阳性

我在模型中定义了以下规则。

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

    ];
}

工作正常。并在我尝试输入 负值 时显示错误消息。

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

如何限制用户不输入-0

谢谢

试试这个:

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

更多详情,你可以看看

嗯,没什么奇怪的,因为 -0 等于 0,您可以简单地使用匹配规则:

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

详细了解 match validator