首页 > 解决方案 > 最小数量验证器在 Yii2 模型中不起作用

问题描述

[['price'], 'integer', 'min' => 0, 'tooSmall' => 'Price cannot be less than 0'],

我的模型文件中有上述规则。但是在验证模型时,回报是作为价格true传递的。NULL

这有什么理由吗?

标签: phpmodelyii2

解决方案


如果您想在字段为空时进行验证,请设置为(默认情况下设置为skipOnEmpty)。falsetrue

[['price'], 'integer', 'skipOnEmpty' => false, 'min' => 0, 'tooSmall' => 'Price cannot be less than 0'],

推荐阅读