PostRequest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace App\Http\Requests;
  3. use Illuminate\Foundation\Http\FormRequest;
  4. use App\Rules\TinyMCERule;
  5. use App\Rules\AllowTagString;
  6. use App\Rules\NormalString;
  7. use App\Http\Traits\BoardTrait;
  8. class PostRequest extends FormRequest
  9. {
  10. use BoardTrait;
  11. /**
  12. * Determine if the user is authorized to make this request.
  13. *
  14. * @return bool
  15. */
  16. public function authorize()
  17. {
  18. return true;
  19. }
  20. /**
  21. * Get the validation rules that apply to the request.
  22. *
  23. * @return array
  24. */
  25. public function rules()
  26. {
  27. $bid = $this->post('bid');
  28. $pid = $this->post('pid');
  29. $boardMeta = $this->boardMeta;
  30. $rules = [
  31. 'bid' => 'required|numeric|exists:tb_board,id',
  32. 'pid' => 'nullable|numeric',
  33. 'is_speaker' => 'numeric|in:0,1',
  34. 'is_notice' => 'numeric|in:0,1',
  35. 'is_secret' => 'numeric|in:0,1',
  36. 'subject' => ['required', 'string'],
  37. 'content' => ['required', new TinyMCERule],
  38. ];
  39. if($pid) {
  40. $rules['pid'] = 'required|exists:tb_post,id';
  41. }
  42. if(($postSubjectMinLength = $boardMeta->item('post_subject_min_length', 0)) > 0) {
  43. $rules['subject'][] = "min:$postSubjectMinLength";
  44. }
  45. if(($postSubjectMaxLength = $boardMeta->item('post_subject_max_length', 0)) > 0) {
  46. $rules['subject'][] = "max:$postSubjectMaxLength";
  47. }
  48. if(($postContentMinLength = $boardMeta->item('post_content_min_length', 0)) > 0) {
  49. $rules['content'][] = "min:$postContentMinLength";
  50. }
  51. if(($postContentMaxLength = $boardMeta->item('post_content_max_length', 0)) > 0) {
  52. $rules['content'][] = "max:$postContentMaxLength";
  53. }
  54. // 자동등록방지문자 확인
  55. if($boardMeta->item('use_post_captcha', 0)) {
  56. $rules['captcha'] = 'required|valid_captcha';
  57. }
  58. // 분류
  59. if($boardMeta->item('use_category', 0))
  60. {
  61. $rules['category'][] = 'exists:tb_board_category,id';
  62. // 분류 필수값 선택 사용시
  63. $rules['category'][] = ($boardMeta->item('use_category_required', 0) ? 'required' : 'nullable');
  64. }
  65. // Tag
  66. if($boardMeta->item('use_post_tag', 0)) {
  67. $rules['tags'] = new AllowTagString;
  68. }
  69. // Link
  70. if($boardMeta->item('link_num', 0) > 0) {
  71. $rules['links.*'] = 'nullable|url';
  72. }
  73. // 파일
  74. if($boardMeta->item('use_upload_file', 0)) {
  75. if(($uploadFileMaxSize = $boardMeta->item('upload_file_max_size', 0)) > 0) {
  76. $rules['files.*'] = 'file|max:' . ($uploadFileMaxSize * 1024 * 1024);
  77. }
  78. }
  79. // 비회원 유효성 검사
  80. if(!$this->user()) {
  81. $rules['username'] = ['required', 'min:2', 'max:10', 'string', new NormalString];
  82. $rules['password'] = 'required|min:3|max:10';
  83. }
  84. return $rules;
  85. }
  86. /**
  87. * Get custom attributes for validator errors.
  88. *
  89. * @return array
  90. */
  91. public function attributes()
  92. {
  93. return [
  94. 'bid' => '게시판 PK',
  95. 'pid' => '게시글 PK',
  96. 'is_speaker' => '전체공지',
  97. 'is_notice' => '공지사항',
  98. 'is_secret' => '비밀글',
  99. 'username' => '이름',
  100. 'password' => '비밀번호',
  101. 'category' => '분류',
  102. 'subject' => '제목',
  103. 'content' => '내용',
  104. 'tag' => 'Tag',
  105. 'link.*' => 'Link',
  106. 'file.*' => 'File',
  107. 'captcha' => '자동등록방지문자'
  108. ];
  109. }
  110. /**
  111. * Get the error messages for the defined validation rules.
  112. *
  113. * @return array
  114. */
  115. public function messages()
  116. {
  117. return [
  118. 'username.required' => '이름을 입력하세요.',
  119. 'username.min' => '이름은 :min자 이상 입력하세요.',
  120. 'username.max' => '이름은 :max자 이하 입력하세요.',
  121. 'username.string' => '이름 형식이 옳지 않습니다.',
  122. 'password.required' => '비밀번호를 입력하세요.',
  123. 'password.min' => '비밀번호는 :min자 이상 입력하세요.',
  124. 'password.max' => '비밀번호는 :max자 이하 입력하세요.',
  125. 'category.required' => '분류를 선택하세요.',
  126. 'subject.required' => '제목을 입력하세요.',
  127. 'content.required' => '내용을 입력하세요.',
  128. 'link.required' => '`Link`를 입력하세요.',
  129. 'link.url' => '`Link`형식이 옳지 않습니다.',
  130. 'file.required' => '`File`를 첨부하세요.',
  131. 'file.file' => '`File`형식이 옳지 않습니다.',
  132. 'file.*.size' => '첨부 가능한 파일의 최대 크기는 :sizeMB 입니다.',
  133. 'captcha.required' => '자동등록방지 문자를 입력하세요.',
  134. 'captcha.valid_captcha' => '자동등록방지 문자 유효성 검사에 실패했습니다. 다시 시도해 주세요.'
  135. ];
  136. }
  137. }