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.

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