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.

108 lines
3.3 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
  1. <?php
  2. namespace App\Http\Requests;
  3. class CommentRequest extends BaseRequest
  4. {
  5. //总的验证规则
  6. protected $rules = [
  7. 'id' => 'required|integer',
  8. 'comment_type' => 'required|integer|between:0,1',
  9. 'id_value' => 'required|integer',
  10. 'comment_rank' => 'integer|between:1,5',
  11. 'add_time' => 'required|integer',
  12. 'ip_address' => 'max:20',
  13. 'status' => 'integer|between:0,1',
  14. 'parent_id' => 'integer',
  15. 'user_id' => 'required|integer',
  16. 'is_anonymous' => 'integer|between:0,1',
  17. ];
  18. //总的自定义错误信息
  19. protected $messages = [
  20. 'id.required' => 'ID必填',
  21. 'id.integer' => 'ID必须为数字',
  22. 'comment_type.required' => '用户评论的类型必填',
  23. 'comment_type.integer' => '用户评论的类型必须为数字',
  24. 'comment_type.between' => '用户评论的类型;0评论的是商品,1评论的是文章',
  25. 'id_value.required' => '文章或者商品的id必填',
  26. 'id_value.integer' => '文章或者商品的id必须为数字',
  27. 'comment_rank.integer' => '评价等级必须为数字',
  28. 'comment_rank.between' => '评价等级,1到5星',
  29. 'add_time.required' => '添加时间必填',
  30. 'add_time.integer' => '添加时间必须是数字',
  31. 'ip_address.max' => 'IP地址不能超过20个字符',
  32. 'status.integer' => '状态必须是数字',
  33. 'status.between' => '是否显示;1是;0隐藏',
  34. 'parent_id.integer' => '评论的父节点必须为数字',
  35. 'user_id.required' => '用户ID必填',
  36. 'user_id.integer' => '用户ID必须为数字',
  37. 'is_anonymous.integer' => '是否匿名必须是数字',
  38. 'is_anonymous.between' => '是否匿名,0否',
  39. ];
  40. //场景验证规则
  41. protected $scene = [
  42. 'add' => ['comment_type', 'id_value', 'comment_rank', 'ip_address', 'status', 'parent_id', 'user_id', 'is_anonymous'],
  43. 'edit' => ['comment_type', 'id_value', 'comment_rank', 'ip_address', 'status', 'parent_id', 'user_id', 'is_anonymous'],
  44. 'del' => ['id'],
  45. ];
  46. /**
  47. * Determine if the user is authorized to make this request.
  48. *
  49. * @return bool
  50. */
  51. public function authorize()
  52. {
  53. return true; //修改为true
  54. }
  55. /**
  56. * Get the validation rules that apply to the request.
  57. *
  58. * @return array
  59. */
  60. public function rules()
  61. {
  62. return $this->rules;
  63. }
  64. /**
  65. * 获取被定义验证规则的错误消息.
  66. *
  67. * @return array
  68. */
  69. public function messages()
  70. {
  71. return $this->messages;
  72. }
  73. //获取场景验证规则
  74. public function getSceneRules($name, $fields = null)
  75. {
  76. $res = array();
  77. if(!isset($this->scene[$name]))
  78. {
  79. return false;
  80. }
  81. $scene = $this->scene[$name];
  82. if($fields != null && is_array($fields))
  83. {
  84. $scene = $fields;
  85. }
  86. foreach($scene as $k=>$v)
  87. {
  88. if(isset($this->rules[$v])){$res[$v] = $this->rules[$v];}
  89. }
  90. return $res;
  91. }
  92. //获取场景验证规则自定义错误信息
  93. public function getSceneRulesMessages()
  94. {
  95. return $this->messages;
  96. }
  97. }