[]], ['riseSwitch', 'default', 'value' => 1], // 数据类型验证 // ['festIdList', 'validateFestIdList'], ['riseSwitch', 'in', 'range' => [0, 1]], ['riseType', 'in', 'range' => [0, 1]], ['riseAmount', 'validateRiseAmount'], ]; } /** * 校验涨价金额或比例,允许 0,但不允许空值、非数字和负数。 * @param string $attribute 属性名 * @param array $params 参数 */ public function validateRiseAmount($attribute, $params) { $value = $this->$attribute; if ($this->riseSwitch != 1 && ($value === null || $value === '')) { return; } if ($value === null || $value === '') { $this->addError($attribute, '请设置涨价金额'); return; } if (!is_scalar($value)) { $this->addError($attribute, '涨价金额/百分比必须为数字'); return; } $value = trim((string)$value); if ($value === '') { $this->addError($attribute, '请设置涨价金额'); return; } if (!is_numeric($value)) { $this->addError($attribute, '涨价金额/百分比必须为数字'); return; } if ($value < 0) { $this->addError($attribute, '涨价金额/百分比不能小于0'); } } /** * 验证节日ID列表 * @param string $attribute 属性名 * @param array $params 参数 */ public function validateFestIdList($attribute, $params) { if (!empty($this->$attribute)) { if (!is_array($this->$attribute)) { $this->addError($attribute, '节日列表格式不正确'); return; } foreach ($this->$attribute as $festId) { if (!is_numeric($festId) || $festId <= 0) { $this->addError($attribute, '节日ID必须为正整数'); return; } } } } /** * 获取属性标签 * @return array */ public function attributeLabels() { return [ 'festIdList' => '节日列表', 'riseSwitch' => '涨价开关', 'riseType' => '涨价类型', 'riseAmount' => '涨价金额/百分比', ]; } }