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.

100 lines
2.6 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Requests;
  3. class GuestbookRequest extends BaseRequest
  4. {
  5. //总的验证规则
  6. protected $rules = [
  7. 'id' => 'required|integer',
  8. 'title' => 'required|max:150',
  9. 'addtime' => 'required|integer',
  10. 'msg' => 'required|max:250',
  11. 'status' => 'integer|between:0,1',
  12. 'name' => 'max:30',
  13. 'phone' => 'max:20',
  14. 'email' => 'max:60',
  15. ];
  16. //总的自定义错误信息
  17. protected $messages = [
  18. 'id.required' => 'ID必填',
  19. 'id.integer' => 'ID必须为数字',
  20. 'title.required' => '标题必填',
  21. 'title.max' => '标题不能超过150个字符',
  22. 'addtime.required' => '添加时间必填',
  23. 'addtime.integer' => '添加时间必须是数字',
  24. 'msg.required' => '描述必填',
  25. 'msg.max' => '描述不能超过250个字符',
  26. 'status.integer' => '状态必须是数字',
  27. 'status.between' => '是否阅读,默认0未阅读',
  28. 'name.max' => '姓名不能超过30个字符',
  29. 'phone.max' => '电话不能超过20个字符',
  30. 'email.max' => '邮箱不能超过60个字符',
  31. ];
  32. //场景验证规则
  33. protected $scene = [
  34. 'add' => ['title', 'addtime', 'msg', 'status', 'name', 'phone', 'email'],
  35. 'edit' => ['title', 'addtime', 'msg', 'status', 'name', 'phone', 'email'],
  36. 'del' => ['id'],
  37. ];
  38. /**
  39. * Determine if the user is authorized to make this request.
  40. *
  41. * @return bool
  42. */
  43. public function authorize()
  44. {
  45. return true; //修改为true
  46. }
  47. /**
  48. * Get the validation rules that apply to the request.
  49. *
  50. * @return array
  51. */
  52. public function rules()
  53. {
  54. return $this->rules;
  55. }
  56. /**
  57. * 获取被定义验证规则的错误消息.
  58. *
  59. * @return array
  60. */
  61. public function messages()
  62. {
  63. return $this->messages;
  64. }
  65. //获取场景验证规则
  66. public function getSceneRules($name, $fields = null)
  67. {
  68. $res = array();
  69. if(!isset($this->scene[$name]))
  70. {
  71. return false;
  72. }
  73. $scene = $this->scene[$name];
  74. if($fields != null && is_array($fields))
  75. {
  76. $scene = $fields;
  77. }
  78. foreach($scene as $k=>$v)
  79. {
  80. if(isset($this->rules[$v])){$res[$v] = $this->rules[$v];}
  81. }
  82. return $res;
  83. }
  84. //获取场景验证规则自定义错误信息
  85. public function getSceneRulesMessages()
  86. {
  87. return $this->messages;
  88. }
  89. }