| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace hd\models\goods;
- use hd\models\BaseForm;
- /**
- * 商品涨价设置表单模型
- */
- class UpdateRiseForm extends BaseForm
- {
- /**
- * 节日ID列表
- * @var array
- */
- public $festIdList;
- /**
- * 涨价开关
- * @var int
- */
- public $riseSwitch;
- /**
- * 涨价类型 (0=金额, 1=百分比)
- * @var int
- */
- public $riseType;
- /**
- * 涨价金额或百分比
- * @var float
- */
- public $riseAmount;
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- // 默认值设置
- ['festIdList', 'default', 'value' => []],
- ['riseSwitch', 'default', 'value' => 1],
-
- // 数据类型验证
- // ['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' => '请设置涨价金额'],
- ];
- }
- /**
- * 验证节日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' => '涨价金额/百分比',
- ];
- }
- }
|