SendHbRuleItemForm.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace hd\models\hb;
  3. use hd\models\BaseForm;
  4. /**
  5. * 充值送红包规则项表单模型
  6. */
  7. class SendHbRuleItemForm extends BaseForm
  8. {
  9. /** @var int|string 规则ID:新增传空字符串,更新传正整数 */
  10. public $id;
  11. /** @var int 充值金额 */
  12. public $amount;
  13. /** @var int 红包金额 */
  14. public $hbAmount;
  15. /** @var int 红包张数 */
  16. public $num;
  17. /** @var int 最低消费金额(0 表示不限制) */
  18. public $miniCost;
  19. /** @var int 有效天数(0 表示永不过期) */
  20. public $duration;
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function rules()
  25. {
  26. return [
  27. [['id'], 'default', 'value' => ''],
  28. [['num'], 'default', 'value' => 1],
  29. [['miniCost', 'duration'], 'default', 'value' => 0],
  30. [['amount'], 'required', 'message' => '请填写充值金额'],
  31. [['hbAmount'], 'required', 'message' => '请填写红包金额'],
  32. // 注意:不要对字段做 intval 过滤,避免 "abc" 被转成 0 导致校验绕过
  33. [['amount', 'hbAmount'], 'number', 'min' => 0.01, 'tooSmall' => '{attribute}必须大于等于0.01'],
  34. [['num'], 'integer', 'message' => '{attribute}必须为正整数', 'min' => 1, 'tooSmall' => '{attribute}最小不能小于1'],
  35. [['miniCost'], 'integer', 'message' => '{attribute}必须为正整数', 'min' => 0, 'tooSmall' => '{attribute}不能小于0'],
  36. [['duration'], 'integer', 'min' => 0, 'max' => 3650, 'tooSmall' => '{attribute}不能小于0', 'tooBig' => '{attribute}过大'],
  37. [['id'], 'validateId'],
  38. ];
  39. }
  40. /**
  41. * 新增时 id 允许为空字符串;更新时必须为正整数。
  42. *
  43. * @param string $attribute
  44. */
  45. public function validateId($attribute)
  46. {
  47. $value = $this->$attribute;
  48. if ($value === '' || $value === null) {
  49. return;
  50. }
  51. if (is_int($value)) {
  52. if ($value < 1) {
  53. $this->addError($attribute, '规则ID参数错误');
  54. }
  55. return;
  56. }
  57. if (is_string($value) && ctype_digit($value)) {
  58. $intVal = (int)$value;
  59. if ($intVal < 1) {
  60. $this->addError($attribute, '规则ID参数错误');
  61. return;
  62. }
  63. $this->$attribute = $intVal;
  64. return;
  65. }
  66. $this->addError($attribute, '规则ID参数错误');
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function attributeLabels()
  72. {
  73. return [
  74. 'id' => '规则ID',
  75. 'amount' => '充值金额',
  76. 'hbAmount' => '红包金额',
  77. 'num' => '红包张数',
  78. 'miniCost' => '最低消费金额',
  79. 'duration' => '有效天数',
  80. ];
  81. }
  82. }