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.

88 lines
2.0 KiB

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