You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.0 KiB

  1. <?php
  2. namespace App\Http\Requests;
  3. class LogRequest extends BaseRequest
  4. {
  5. //总的验证规则
  6. protected $rules = [
  7. 'id' => 'required|integer',
  8. 'ip' => 'required|max:15|ip',
  9. 'content' => 'max:250',
  10. 'login_name' => 'max:30',
  11. 'login_id' => 'integer',
  12. 'url' => 'required|max:255',
  13. 'domain_name' => 'max:60',
  14. 'http_referer' => 'max:255',
  15. 'http_method' => 'required|max:10',
  16. 'add_time' => 'required|integer',
  17. ];
  18. //总的自定义错误信息
  19. protected $messages = [
  20. 'id.required' => 'ID不能为空',
  21. 'id.integer' => 'ID必须是数字',
  22. 'ip.required' => 'IP不能为空',
  23. 'ip.max' => 'IP不能超过15个字符',
  24. 'ip.ip' => 'IP格式不正确',
  25. 'content.max' => '操作内容不能超过255个字符',
  26. 'login_name.required' => '登录名称不能为空',
  27. 'login_name.max' => '登录名称不能超过30个字符',
  28. 'login_id.required' => '登录ID不能为空',
  29. 'login_id.integer' => '登录ID必须是数字',
  30. 'url.required' => 'URL不能为空',
  31. 'url.max' => 'URL不能超过255个字符',
  32. 'domain_name.max' => '域名不能超过60个字符',
  33. 'http_referer.max' => '上一个页面URL不能超过250个字符',
  34. 'http_method.required' => '请求方式不能为空',
  35. 'http_method.max' => '请求方式不能超过10个字符',
  36. 'add_time.required' => '添加时间不能为空',
  37. 'add_time.integer' => '添加时间格式不正确',
  38. ];
  39. //场景验证规则
  40. protected $scene = [
  41. 'add' => ['ip', 'content', 'login_name', 'login_id', 'route', 'http_method', 'add_time'],
  42. 'edit' => ['ip', 'content', 'login_name', 'login_id', 'route', 'http_method', 'add_time'],
  43. 'del' => ['id'],
  44. ];
  45. /**
  46. * Determine if the user is authorized to make this request.
  47. *
  48. * @return bool
  49. */
  50. public function authorize()
  51. {
  52. return true; //修改为true
  53. }
  54. /**
  55. * Get the validation rules that apply to the request.
  56. *
  57. * @return array
  58. */
  59. public function rules()
  60. {
  61. return $this->rules;
  62. }
  63. /**
  64. * 获取被定义验证规则的错误消息.
  65. *
  66. * @return array
  67. */
  68. public function messages()
  69. {
  70. return $this->messages;
  71. }
  72. //获取场景验证规则
  73. public function getSceneRules($name, $fields = null)
  74. {
  75. $res = array();
  76. if(!isset($this->scene[$name]))
  77. {
  78. return false;
  79. }
  80. $scene = $this->scene[$name];
  81. if($fields != null && is_array($fields))
  82. {
  83. $scene = $fields;
  84. }
  85. foreach($scene as $k=>$v)
  86. {
  87. if(isset($this->rules[$v])){$res[$v] = $this->rules[$v];}
  88. }
  89. return $res;
  90. }
  91. //获取场景验证规则自定义错误信息
  92. public function getSceneRulesMessages()
  93. {
  94. return $this->messages;
  95. }
  96. }