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.

109 lines
3.4 KiB

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' => 'required|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.required' => '评价等级必填',
  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. ];
  41. //场景验证规则
  42. protected $scene = [
  43. 'add' => ['comment_type', 'id_value', 'comment_rank', 'add_time', 'ip_address', 'status', 'parent_id', 'user_id', 'is_anonymous'],
  44. 'edit' => ['comment_type', 'id_value', 'comment_rank', 'add_time', 'ip_address', 'status', 'parent_id', 'user_id', 'is_anonymous'],
  45. 'del' => ['id'],
  46. ];
  47. /**
  48. * Determine if the user is authorized to make this request.
  49. *
  50. * @return bool
  51. */
  52. public function authorize()
  53. {
  54. return true; //修改为true
  55. }
  56. /**
  57. * Get the validation rules that apply to the request.
  58. *
  59. * @return array
  60. */
  61. public function rules()
  62. {
  63. return $this->rules;
  64. }
  65. /**
  66. * 获取被定义验证规则的错误消息.
  67. *
  68. * @return array
  69. */
  70. public function messages()
  71. {
  72. return $this->messages;
  73. }
  74. //获取场景验证规则
  75. public function getSceneRules($name, $fields = null)
  76. {
  77. $res = array();
  78. if(!isset($this->scene[$name]))
  79. {
  80. return false;
  81. }
  82. $scene = $this->scene[$name];
  83. if($fields != null && is_array($fields))
  84. {
  85. $scene = $fields;
  86. }
  87. foreach($scene as $k=>$v)
  88. {
  89. if(isset($this->rules[$v])){$res[$v] = $this->rules[$v];}
  90. }
  91. return $res;
  92. }
  93. //获取场景验证规则自定义错误信息
  94. public function getSceneRulesMessages()
  95. {
  96. return $this->messages;
  97. }
  98. }