UpdateRiseForm.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace hd\models\goods;
  3. use hd\models\BaseForm;
  4. /**
  5. * 商品涨价设置表单模型
  6. */
  7. class UpdateRiseForm extends BaseForm
  8. {
  9. /**
  10. * 节日ID列表
  11. * @var array
  12. */
  13. public $festIdList;
  14. /**
  15. * 涨价开关
  16. * @var int
  17. */
  18. public $riseSwitch;
  19. /**
  20. * 涨价类型 (0=金额, 1=百分比)
  21. * @var int
  22. */
  23. public $riseType;
  24. /**
  25. * 涨价金额或百分比
  26. * @var float
  27. */
  28. public $riseAmount;
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. // 默认值设置
  36. ['festIdList', 'default', 'value' => []],
  37. ['riseSwitch', 'default', 'value' => 1],
  38. // 数据类型验证
  39. // ['festIdList', 'validateFestIdList'],
  40. ['riseSwitch', 'in', 'range' => [0, 1]],
  41. ['riseType', 'in', 'range' => [0, 1]],
  42. ['riseAmount', 'number', 'min' => 0],
  43. // 条件验证 - 当涨价开关打开时,必须选择节日
  44. // ['festIdList', 'required', 'when' => function($model) {
  45. // return $model->riseSwitch == 1;
  46. // }, 'message' => '请选择节日'],
  47. // 条件验证 - 当涨价开关打开时,必须设置涨价金额
  48. ['riseAmount', 'required', 'when' => function($model) {
  49. return $model->riseSwitch == 1;
  50. }, 'message' => '请设置涨价金额'],
  51. ];
  52. }
  53. /**
  54. * 验证节日ID列表
  55. * @param string $attribute 属性名
  56. * @param array $params 参数
  57. */
  58. public function validateFestIdList($attribute, $params)
  59. {
  60. if (!empty($this->$attribute)) {
  61. if (!is_array($this->$attribute)) {
  62. $this->addError($attribute, '节日列表格式不正确');
  63. return;
  64. }
  65. foreach ($this->$attribute as $festId) {
  66. if (!is_numeric($festId) || $festId <= 0) {
  67. $this->addError($attribute, '节日ID必须为正整数');
  68. return;
  69. }
  70. }
  71. }
  72. }
  73. /**
  74. * 获取属性标签
  75. * @return array
  76. */
  77. public function attributeLabels()
  78. {
  79. return [
  80. 'festIdList' => '节日列表',
  81. 'riseSwitch' => '涨价开关',
  82. 'riseType' => '涨价类型',
  83. 'riseAmount' => '涨价金额/百分比',
  84. ];
  85. }
  86. }