teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

試行錯誤した記録を追加

2019/09/17 02:29

投稿

shoco
shoco

スコア170

title CHANGED
File without changes
body CHANGED
@@ -59,7 +59,34 @@
59
59
 
60
60
  }
61
61
 
62
+ ```
63
+ 試行錯誤内容としましては
64
+ ```
65
+ ・既存のFormRequestクラスのfailedValidationメソッドを下のように変えても
66
+
67
+ protected function failedValidation(Validator $validator)
68
+ {
69
+ throw (new ValidationException($validator))
70
+ ->errorBag($this->errorBag)
71
+ ->redirectTo($this->getRedirectUrl());
72
+ }
73
+
74
+
75
+
76
+ protected function failedValidation(Validator $validator)
77
+ {
78
+ $response['code'] = 'XXX';
79
+
80
+ if($validator->errors()->has($xxxxxxxxx)) {
81
+ $response['message'] = $validator->errors()->first($xxxxxxxxx);
82
+ throw new HttpResponseException(response()->json($response, 422));
83
+ }
84
+ }
85
+ $xxxxxxxxxの箇所を汎用的に使用できる変数にして
86
+ この部品だけですべてのすべてのバリデーション処理ができるようにさせたい
87
+ と色々試しております。
88
+
89
+ ```
90
+
62
91
  コーディング簡略化に自信のある人がいましたら
63
- よろしくお願いします。
92
+ よろしくお願いします。
64
-
65
- ```