DonateValidator.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace App\Http\Validator;
  3. class DonateValidator extends BaseValidator
  4. {
  5. // 当前模型所有验证规则
  6. public static $rules = [
  7. 'id' => 'required',
  8. 'title' => 'required|string|min:1|max:20',
  9. 'phone' => 'required|string|min:1|max:20',
  10. 'stock' => 'required|min:1|max:10',
  11. 'thumb' => 'required|string|min:2|max:250',
  12. 'description' => 'required|string|min:2|max:500',
  13. ];
  14. // 当前模型所有错误提示信息
  15. public static $msgs = [
  16. 'required' => ':attribute不能为空',
  17. 'string' => ':attribute必须是字符串',
  18. 'min' => ':attribute长度不能小于:min位',
  19. 'max' => ':attribute长度不能大于:max位',
  20. 'exists' => ':attribute不存在',
  21. 'rule' => ':attribute格式不正确',
  22. ];
  23. // 当前模型所有验证字段
  24. public static $fields = [
  25. 'id' => 'ID',
  26. 'phone' => '联系方式',
  27. 'stock' => '数量',
  28. 'thumb' => '图片',
  29. 'title' => '物品名称',
  30. 'description' => '物品描述',
  31. ];
  32. // 当前模型所有验证场景
  33. public static $scenes = [
  34. 'info'=> ['id'],
  35. 'send'=> ['title','phone','stock','description','thumb'],
  36. ];
  37. /**
  38. * 验证
  39. * @param $request
  40. * @param string $scene
  41. * @return int|mixed
  42. */
  43. public static function check($request, $scene=''){
  44. $validator = new BaseValidator(self::$rules, self::$msgs, self::$fields, self::$scenes);
  45. return $validator->checkParams($request, $scene);
  46. }
  47. }