| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace hd\models\hb;
- use hd\models\BaseForm;
- /**
- * 充值送红包规则项表单模型
- */
- class SendHbRuleItemForm extends BaseForm
- {
- /** @var int|string 规则ID:新增传空字符串,更新传正整数 */
- public $id;
- /** @var int 充值金额 */
- public $amount;
- /** @var int 红包金额 */
- public $hbAmount;
- /** @var int 红包张数 */
- public $num;
- /** @var int 最低消费金额(0 表示不限制) */
- public $miniCost;
- /** @var int 有效天数(0 表示永不过期) */
- public $duration;
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['id'], 'default', 'value' => ''],
- [['num'], 'default', 'value' => 1],
- [['miniCost', 'duration'], 'default', 'value' => 0],
- [['amount'], 'required', 'message' => '请填写充值金额'],
- [['hbAmount'], 'required', 'message' => '请填写红包金额'],
- // 注意:不要对字段做 intval 过滤,避免 "abc" 被转成 0 导致校验绕过
- [['amount', 'hbAmount'], 'number', 'min' => 0.01, 'tooSmall' => '{attribute}必须大于等于0.01'],
- [['num'], 'integer', 'message' => '{attribute}必须为正整数', 'min' => 1, 'tooSmall' => '{attribute}最小不能小于1'],
- [['miniCost'], 'integer', 'message' => '{attribute}必须为正整数', 'min' => 0, 'tooSmall' => '{attribute}不能小于0'],
- [['duration'], 'integer', 'min' => 0, 'max' => 3650, 'tooSmall' => '{attribute}不能小于0', 'tooBig' => '{attribute}过大'],
- [['id'], 'validateId'],
- ];
- }
- /**
- * 新增时 id 允许为空字符串;更新时必须为正整数。
- *
- * @param string $attribute
- */
- public function validateId($attribute)
- {
- $value = $this->$attribute;
- if ($value === '' || $value === null) {
- return;
- }
- if (is_int($value)) {
- if ($value < 1) {
- $this->addError($attribute, '规则ID参数错误');
- }
- return;
- }
- if (is_string($value) && ctype_digit($value)) {
- $intVal = (int)$value;
- if ($intVal < 1) {
- $this->addError($attribute, '规则ID参数错误');
- return;
- }
- $this->$attribute = $intVal;
- return;
- }
- $this->addError($attribute, '规则ID参数错误');
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '规则ID',
- 'amount' => '充值金额',
- 'hbAmount' => '红包金额',
- 'num' => '红包张数',
- 'miniCost' => '最低消费金额',
- 'duration' => '有效天数',
- ];
- }
- }
|