WorkValidator.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App\Http\Validator;
  3. class WorkValidator 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. 'reason' => 'nullable|string|min:2|max:150',
  11. ];
  12. // 当前模型所有错误提示信息
  13. public static $msgs = [
  14. 'required' => ':attribute不能为空',
  15. 'string' => ':attribute必须是字符串',
  16. 'min' => ':attribute长度不能小于:min位',
  17. 'max' => ':attribute长度不能大于:max位',
  18. 'exists' => ':attribute不存在',
  19. 'rule' => ':attribute格式不正确',
  20. ];
  21. // 当前模型所有验证字段
  22. public static $fields = [
  23. 'id' => 'ID',
  24. 'realname' => '姓名',
  25. 'phone' => '联系电话',
  26. ];
  27. // 当前模型所有验证场景
  28. public static $scenes = [
  29. 'info'=> ['id'],
  30. 'check'=> ['id','reason'],
  31. 'books'=> ['id','realname','phone'],
  32. ];
  33. /**
  34. * 验证
  35. * @param $request
  36. * @param string $scene
  37. * @return int|mixed
  38. */
  39. public static function check($request, $scene=''){
  40. $validator = new BaseValidator(self::$rules, self::$msgs, self::$fields, self::$scenes);
  41. return $validator->checkParams($request, $scene);
  42. }
  43. }