CommentRequest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace App\Http\Requests;
  3. use Illuminate\Foundation\Http\FormRequest;
  4. use Illuminate\Contracts\Validation\Validator;
  5. use Illuminate\Validation\ValidationException;
  6. use Illuminate\Http\Exceptions\HttpResponseException;
  7. use Illuminate\Http\JsonResponse;
  8. use App\Http\Traits\BoardTrait;
  9. use App\Rules\NormalString;
  10. use App\Rules\FilterSpamKeyword;
  11. use App\Rules\BlobDataEscape;
  12. class CommentRequest extends FormRequest
  13. {
  14. use BoardTrait;
  15. /**
  16. * Determine if the user is authorized to make this request.
  17. *
  18. * @return bool
  19. */
  20. public function authorize()
  21. {
  22. return true;
  23. }
  24. /**
  25. * Get the validation rules that apply to the request.
  26. *
  27. * @return array
  28. */
  29. public function rules()
  30. {
  31. $boardID = $this->post('bid');
  32. $commentID = $this->post('cid');
  33. $boardMeta = $this->boardMeta;
  34. $rules = [
  35. 'bid' => 'required|exists:tb_board,id',
  36. 'pid' => 'required|exists:tb_post,id',
  37. 'content' => ['required', new FilterSpamKeyword],
  38. 'is_secret' => 'nullable|numeric|in:0,1'
  39. ];
  40. if($commentID) {
  41. $rules['cid'] = 'required|exists:tb_comment,id';
  42. }
  43. if(($commentMinLength = $boardMeta->item('comment_min_length', 0)) > 0) {
  44. $rules['content'][] = "min:$commentMinLength";
  45. }
  46. if(($commentMaxLength = $boardMeta->item('comment_max_length', 0)) > 0) {
  47. if($boardMeta->item('use_personal', 0)) {
  48. $rules['content'][] = new BlobDataEscape($boardID);
  49. }else{
  50. $rules['content'][] = "max:$commentMaxLength";
  51. }
  52. }
  53. // 비회원 유효성 검사
  54. if(!$this->user()) {
  55. $rules['username'] = ['required', 'min:2', 'max:10', 'string', new NormalString];
  56. $rules['password'] = 'required|min:3|max:10';
  57. }
  58. return $rules;
  59. }
  60. /**
  61. * Get custom attributes for validator errors.
  62. *
  63. * @return array
  64. */
  65. public function attributes()
  66. {
  67. return [
  68. 'bid' => '게시판 PK',
  69. 'pid' => '게시글 PK',
  70. 'username' => '이름',
  71. 'password' => '비밀번호',
  72. 'content' => '댓글',
  73. 'is_secret' => '비밀글'
  74. ];
  75. }
  76. /**
  77. * Get the error messages for the defined validation rules.
  78. *
  79. * @return array
  80. */
  81. public function messages()
  82. {
  83. return [
  84. 'username.required' => '이름을 입력하세요.',
  85. 'username.min' => '이름은 :min자 이상 입력하세요.',
  86. 'username.max' => '이름은 :max자 이하 입력하세요.',
  87. 'username.string' => '이름 형식이 옳지 않습니다.',
  88. 'password.required' => '비밀번호를 입력하세요.',
  89. 'password.min' => '비밀번호는 :min자 이상 입력하세요.',
  90. 'password.max' => '비밀번호는 :max자 이하 입력하세요.',
  91. 'content.required' => '댓글을 입력해주세요.',
  92. 'content.max' => ':attribute은 최대 :max 글자 입력 가능합니다.',
  93. 'is_secret.in' => '비밀글은 0 또는 1 값만 허용됩니다.'
  94. ];
  95. }
  96. /**
  97. * Handle a failed validation attempt.
  98. *
  99. * @param \Illuminate\Contracts\Validation\Validator $validator
  100. * @return void
  101. *
  102. * @throws \Illuminate\Validation\ValidationException
  103. */
  104. protected function failedValidation(Validator $validator)
  105. {
  106. if($this->wantsJson()) {
  107. $errors = (new ValidationException($validator))->errors();
  108. foreach($errors as $err) {
  109. throw new HttpResponseException(
  110. response()->json([
  111. 'message' => $err
  112. ], JsonResponse::HTTP_OK)
  113. );
  114. }
  115. }
  116. parent::failedValidation($validator);
  117. }
  118. }