| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?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', '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' => '涨价金额/百分比',
- ];
- }
- }
|