|
|
@@ -22,7 +22,7 @@ class UpdateRiseForm extends BaseForm
|
|
|
public $riseSwitch;
|
|
|
|
|
|
/**
|
|
|
- * 涨价类型 (0=金额, 1=百分比)
|
|
|
+ * 涨价类型 (0=百分比, 1=金额)
|
|
|
* @var int
|
|
|
*/
|
|
|
public $riseType;
|
|
|
@@ -47,20 +47,50 @@ class UpdateRiseForm extends BaseForm
|
|
|
// ['festIdList', 'validateFestIdList'],
|
|
|
['riseSwitch', 'in', 'range' => [0, 1]],
|
|
|
['riseType', 'in', 'range' => [0, 1]],
|
|
|
- ['riseAmount', 'number', 'min' => 0],
|
|
|
-
|
|
|
- // 条件验证 - 当涨价开关打开时,必须选择节日
|
|
|
- // ['festIdList', 'required', 'when' => function($model) {
|
|
|
- // return $model->riseSwitch == 1;
|
|
|
- // }, 'message' => '请选择节日'],
|
|
|
-
|
|
|
- // 条件验证 - 当涨价开关打开时,必须设置涨价金额
|
|
|
- ['riseAmount', 'required', 'when' => function($model) {
|
|
|
- return $model->riseSwitch == 1;
|
|
|
- }, 'message' => '请设置涨价金额'],
|
|
|
+ ['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 属性名
|