BoardRequest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Http\Requests;
  3. use Illuminate\Foundation\Http\FormRequest;
  4. use App\Http\Traits\PagingTrait;
  5. class BoardRequest extends FormRequest
  6. {
  7. use PagingTrait;
  8. /**
  9. * Determine if the user is authorized to make this request.
  10. *
  11. * @return bool
  12. */
  13. public function authorize()
  14. {
  15. return true;
  16. }
  17. /**
  18. * Get the validation rules that apply to the request.
  19. *
  20. * @return array
  21. */
  22. public function rules()
  23. {
  24. $rules = [
  25. 'category' => 'nullable|exists:tb_board_category,category_id',
  26. 'field' => 'nullable|string|in:1,2,3,4,5',
  27. 'keyword' => 'nullable|string|max:255',
  28. 'page' => 'nullable|numeric',
  29. 'perPage' => 'nullable|numeric|in:10,15,20,30,40,50',
  30. 'sort' => 'nullable|numeric|in:1,2,3,4'
  31. ];
  32. $code = $this->route('code');
  33. $field = $this->query('field'); // 검색구분
  34. $keyword = $this->query('keyword'); // 검색어
  35. $page = $this->query('page', 1); // 페이지 번호
  36. $category = $this->query('category'); // 분류 번호
  37. // 게시글 설정 값
  38. $boardMeta = $this->boardMeta;
  39. // PC/MO 구분 한 페이지당 목록 수
  40. if(DEVICE_TYPE_1) {
  41. $perPage = ($this->query('per_page') ?? $boardMeta->list_per_page);
  42. $pageCount = $boardMeta->list_page_count;
  43. }else{
  44. $perPage = ($this->query('per_page') ?? $boardMeta->list_mobile_per_page);
  45. $pageCount = $boardMeta->list_page_mobile_count;
  46. }
  47. $sort = ($this->query('sort') ?? $boardMeta->list_sort_type); // 정렬 (날짜순, 조회순, 댓글순, 추천순)
  48. $path = route('board.list', $code);
  49. $offset = $this->getPageOffset($page, $perPage); // 페이징 시작 번호
  50. $this->merge([
  51. // 검색 변수
  52. 'category' => $category,
  53. 'field' => $field,
  54. 'keyword' => $keyword,
  55. 'page' => intval($page),
  56. 'perPage' => intval($perPage),
  57. 'pageCount' => intval($pageCount),
  58. 'offset' => $offset,
  59. 'sort' => intval($sort),
  60. 'path' => $path
  61. ]);
  62. return $rules;
  63. }
  64. /**
  65. * Get custom attributes for validator errors.
  66. *
  67. * @return array
  68. */
  69. public function attributes()
  70. {
  71. return [
  72. 'category' => '게시판 종류',
  73. 'field' => '검색 조건',
  74. 'keyword' => '검색어',
  75. 'page' => '목록 번호',
  76. 'perPage' => '목록 수',
  77. 'sort' => '정렬 순서'
  78. ];
  79. }
  80. /**
  81. * Get the error messages for the defined validation rules.
  82. *
  83. * @return array
  84. */
  85. public function messages()
  86. {
  87. return [
  88. 'category.exists' => '게시판이 존재하지 않습니다.',
  89. 'field.in' => '검색 조건이 유효하지 않습니다..',
  90. 'keyword.max' => '검색어는 최대 255글자 입력 가능합니다.',
  91. 'page.numeric' => '목록 번호가 유효하지 않습니다.',
  92. 'perPage.in' => '목록 수가 유효하지 않습니다.',
  93. 'sort.in' => '정렬 순서가 유효하지 않습니다.'
  94. ];
  95. }
  96. }