ArticleValidator.php 1.7 KB

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