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.

93 lines
2.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Requests;
  3. use Illuminate\Http\Request;
  4. class ArticleRequest extends Request
  5. {
  6. //总的验证规则
  7. protected $rules = [
  8. 'id' => 'required|integer',
  9. 'title' => 'required|max:150',
  10. 'typeid' => 'required|integer',
  11. 'click' => ['required','integer'],
  12. ];
  13. //总的自定义错误信息
  14. protected $messages = [
  15. 'title.max' => '标题不能大于150个字',
  16. 'title.required' => '必须填写标题',
  17. 'typeid.required' => '类目ID必填',
  18. 'typeid.integer' => '栏目ID必须为数字',
  19. 'click.required' => '必须填写点击量',
  20. 'click.integer' => '点击必须为数字',
  21. 'id.required' => 'ID必填',
  22. 'id.integer' => 'ID必须为数字',
  23. ];
  24. //场景验证规则
  25. protected $scene = [
  26. 'add' => ['title','typeid','click'],
  27. 'edit' => ['title','typeid'],
  28. 'del' => ['id'],
  29. ];
  30. /**
  31. * Determine if the user is authorized to make this request.
  32. *
  33. * @return bool
  34. */
  35. public function authorize()
  36. {
  37. return true; //修改为true
  38. }
  39. /**
  40. * Get the validation rules that apply to the request.
  41. *
  42. * @return array
  43. */
  44. public function rules()
  45. {
  46. return $this->rules;
  47. }
  48. /**
  49. * 获取被定义验证规则的错误消息.
  50. *
  51. * @return array
  52. */
  53. public function messages()
  54. {
  55. return $this->messages;
  56. }
  57. //获取场景验证规则
  58. public function getSceneRules($name, $fields = null)
  59. {
  60. $res = array();
  61. if(!isset($this->scene[$name]))
  62. {
  63. return false;
  64. }
  65. $scene = $this->scene[$name];
  66. if($fields != null && is_array($fields))
  67. {
  68. $scene = $fields;
  69. }
  70. foreach($scene as $k=>$v)
  71. {
  72. if(isset($this->rules[$v])){$res[$v] = $this->rules[$v];}
  73. }
  74. return $res;
  75. }
  76. //获取场景验证规则自定义错误信息
  77. public function getSceneRulesMessages()
  78. {
  79. return $this->messages;
  80. }
  81. }