''], [['num'], 'default', 'value' => 1], [['miniCost', 'duration'], 'default', 'value' => 0], [['amount'], 'required', 'message' => '请填写充值金额'], [['hbAmount'], 'required', 'message' => '请填写红包金额'], // 注意:不要对字段做 intval 过滤,避免 "abc" 被转成 0 导致校验绕过 [['amount', 'hbAmount'], 'number', 'min' => 0.01, 'tooSmall' => '{attribute}必须大于等于0.01'], [['num'], 'integer', 'message' => '{attribute}必须为正整数', 'min' => 1, 'tooSmall' => '{attribute}最小不能小于1'], [['miniCost'], 'integer', 'message' => '{attribute}必须为正整数', 'min' => 0, 'tooSmall' => '{attribute}不能小于0'], [['duration'], 'integer', 'min' => 0, 'max' => 3650, 'tooSmall' => '{attribute}不能小于0', 'tooBig' => '{attribute}过大'], [['id'], 'validateId'], ]; } /** * 新增时 id 允许为空字符串;更新时必须为正整数。 * * @param string $attribute */ public function validateId($attribute) { $value = $this->$attribute; if ($value === '' || $value === null) { return; } if (is_int($value)) { if ($value < 1) { $this->addError($attribute, '规则ID参数错误'); } return; } if (is_string($value) && ctype_digit($value)) { $intVal = (int)$value; if ($intVal < 1) { $this->addError($attribute, '规则ID参数错误'); return; } $this->$attribute = $intVal; return; } $this->addError($attribute, '规则ID参数错误'); } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => '规则ID', 'amount' => '充值金额', 'hbAmount' => '红包金额', 'num' => '红包张数', 'miniCost' => '最低消费金额', 'duration' => '有效天数', ]; } }