shish 2 bulan lalu
induk
melakukan
af841d28e2

+ 4 - 1
app-hd/controllers/GoodsController.php

@@ -363,6 +363,9 @@ class GoodsController extends BaseController
         $post = $form->attributes;
         $festIdList = isset($post['festIdList']) && !empty($post['festIdList']) ? $post['festIdList'] : [];
         unset($post['festIdList']);
+        $post['riseSwitch'] = (int)$post['riseSwitch'];
+        $post['riseType'] = (int)$post['riseType'];
+        $post['riseAmount'] = $post['riseSwitch'] == 1 ? (float)$post['riseAmount'] : 0;
         // $riseSwitch = isset($post['riseSwitch']) && is_numeric($post['riseSwitch']) ? $post['riseSwitch'] : 1;
         // if ($riseSwitch == 1) {
         //     if (empty($festIdList)) {
@@ -450,4 +453,4 @@ class GoodsController extends BaseController
         util::complete('创建成功');
     }
 
-}
+}

+ 42 - 12
app-hd/models/goods/UpdateRiseForm.php

@@ -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 属性名