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

質問編集履歴

3

試してみたこと2を追記。エラー画像削除

2021/12/08 10:39

投稿

seven_7
seven_7

スコア7

title CHANGED
File without changes
body CHANGED
@@ -61,7 +61,7 @@
61
61
  参考画像:Laravelmanualより
62
62
  ![参考キャプチャ](f9d691f98eb1064642f9da47679423e7.jpeg)
63
63
 
64
- ##試しみたこと
64
+ ##試しみたこと1
65
65
  参考サイトなどをみて、「Player_no_check.php」のようにバリデーションルールを作成したつもりでしたが
66
66
  記述が間違っているようで未定義となっております。
67
67
 
@@ -129,9 +129,55 @@
129
129
  Object of class App\Rules\Player_no_check could not be converted to string
130
130
  ```
131
131
 
132
- C:\xampp\htdocs\fifa_app\vendor\laravel\framework\src\Illuminate\Validation\ValidationRuleParser.php:114
132
+ ##試してみたこと2
133
133
 
134
+ 下記の記述を試したところ「syntax error, unexpected ';', expecting ']'」が表示されました。
135
+ ```Player_no_check.php
136
+ public function passes($attribute, $value)
137
+ {
134
- エラー画像
138
+ return [
139
+ 'player_no' => Player::where('player_no',$value)
135
- 今私がエラーがでましたと言ってみている画像は下記になります。
140
+ ->where('team_owner_id', 1)
141
+ ->doesntExist();
142
+ ];
143
+ }
144
+ ```
136
145
 
146
+ 下記に変更をすると「syntax error」はなくなりましたが記述が合っているか自信がありません。
147
+ ```Player_no_check.php
137
- ![イメージ説明](2fdcf7fae30261ac43cb9fb5020ba446.jpeg)
148
+ public function passes($attribute, $value)
149
+ {
150
+
151
+ return [
152
+ 'player_no' => Player::where('player_no', $value)
153
+ ->where('team_owner_id', 1)->doesntExist(), //「;」を「,」に変更
154
+ ];
155
+ }
156
+ ```
157
+ ```PlayerController
158
+ public function store(Request $request)
159
+ {
160
+ $request->validate([
161
+ 'player_no' => [new Player_no_check],
162
+
163
+ ]);
164
+
165
+ $player = new Player();
166
+ $player->team_owner_id = Auth::id();
167
+ $player->player_no = $request->player_no;
168
+ $player->player_name = $request->player_name;
169
+ $player->save();
170
+ // dd($player);
171
+ return redirect()
172
+ ->route('team_owner.players.index')
173
+ ->with([
174
+ 'message' => '選手登録をしました。',
175
+ 'status' => 'info'
176
+ ]);
177
+ }
178
+
179
+ ```
180
+ この状態で選手の新規登録をおこないました。
181
+
182
+ 結果としては、登録がなんでもできてしまいバリデーションが効いていないような感じになっています。
183
+ 現在のteam_owner_idが1、player_noを10として何度も登録ができてしまう状態です。

2

エラー画像を追加12/8 13:07

2021/12/08 10:39

投稿

seven_7
seven_7

スコア7

title CHANGED
File without changes
body CHANGED
@@ -130,66 +130,8 @@
130
130
  ```
131
131
 
132
132
  C:\xampp\htdocs\fifa_app\vendor\laravel\framework\src\Illuminate\Validation\ValidationRuleParser.php:114
133
- ```エラー場所コード
134
- * @return mixed
135
133
 
136
- */
134
+ エラー画像
135
+ 今私がエラーがでましたと言ってみている画像は下記になります。
137
136
 
138
- protected function prepareRule($rule)
139
-
140
- {
141
-
142
- if ($rule instanceof Closure) {
143
-
144
- $rule = new ClosureValidationRule($rule);
145
-
146
- }
147
-
148
-
149
-
150
- if (! is_object($rule) ||
151
-
152
- $rule instanceof RuleContract ||
153
-
154
- ($rule instanceof Exists && $rule->queryCallbacks()) ||
155
-
156
- ($rule instanceof Unique && $rule->queryCallbacks())) {
157
-
158
- return $rule;
159
-
160
- }
161
-
162
-
163
-
164
- return (string) $rule; //ここでエラーがでている
137
+ ![イメージ説明](2fdcf7fae30261ac43cb9fb5020ba446.jpeg)
165
-
166
- }
167
-
168
-
169
-
170
- /**
171
-
172
- * Define a set of rules that apply to each element in an array attribute.
173
-
174
- *
175
-
176
- * @param array $results
177
-
178
- * @param string $attribute
179
-
180
- * @param string|array $rules
181
-
182
- * @return array
183
-
184
- */
185
-
186
- protected function explodeWildcardRules($results, $attribute, $rules)
187
-
188
- {
189
-
190
- $pattern = str_replace('*', '[^.]*', preg_quote($attribute));
191
-
192
-
193
-
194
- $data = ValidationData::initializeAndGatherData($attribute, $this->data);
195
- ```

1

現在進行中の内容を追記12/8

2021/12/08 04:07

投稿

seven_7
seven_7

スコア7

title CHANGED
File without changes
body CHANGED
@@ -69,4 +69,127 @@
69
69
 
70
70
  どのようにするとバリデーションを希望のバリデーションを通すことができますでしょうか。
71
71
 
72
- どうぞよろしくお願いいたします。
72
+ どうぞよろしくお願いいたします。
73
+
74
+ ##現在でているエラー内容
75
+ 修正後の記述
76
+ ```player_no_check.php
77
+ namespace App\Rules;
78
+
79
+ // use Illuminate\Contracts\Validation\Rule;
80
+ use Illuminate\Validation\Rule;
81
+ use App\Models\Team_owner;
82
+ use App\Models\Player;
83
+
84
+
85
+
86
+ class Player_no_check extends Rule
87
+ {
88
+ /**
89
+ * Create a new rule instance.
90
+ *
91
+ * @return void
92
+ */
93
+ public function __construct()
94
+ {
95
+ //
96
+ }
97
+
98
+ /**
99
+ * Determine if the validation rule passes.
100
+ *
101
+ * @param string $attribute
102
+ * @param mixed $value
103
+ * @return bool
104
+ */
105
+ public function passes($attribute, $value)
106
+ {
107
+ dd($attribute, $value);
108
+ return [
109
+ 'player_no' => Rule::unique('players')->where(function ($query) {
110
+ return $query->where('team_owner_id', 1);
111
+ })
112
+ ];
113
+ }
114
+
115
+ /**
116
+ * Get the validation error message.
117
+ *
118
+ * @return string
119
+ */
120
+ public function message()
121
+ {
122
+ return 'The validation error message.';
123
+ }
124
+ }
125
+ ```
126
+
127
+ ```エラー
128
+ Error
129
+ Object of class App\Rules\Player_no_check could not be converted to string
130
+ ```
131
+
132
+ C:\xampp\htdocs\fifa_app\vendor\laravel\framework\src\Illuminate\Validation\ValidationRuleParser.php:114
133
+ ```エラー場所コード
134
+ * @return mixed
135
+
136
+ */
137
+
138
+ protected function prepareRule($rule)
139
+
140
+ {
141
+
142
+ if ($rule instanceof Closure) {
143
+
144
+ $rule = new ClosureValidationRule($rule);
145
+
146
+ }
147
+
148
+
149
+
150
+ if (! is_object($rule) ||
151
+
152
+ $rule instanceof RuleContract ||
153
+
154
+ ($rule instanceof Exists && $rule->queryCallbacks()) ||
155
+
156
+ ($rule instanceof Unique && $rule->queryCallbacks())) {
157
+
158
+ return $rule;
159
+
160
+ }
161
+
162
+
163
+
164
+ return (string) $rule; //ここでエラーがでている
165
+
166
+ }
167
+
168
+
169
+
170
+ /**
171
+
172
+ * Define a set of rules that apply to each element in an array attribute.
173
+
174
+ *
175
+
176
+ * @param array $results
177
+
178
+ * @param string $attribute
179
+
180
+ * @param string|array $rules
181
+
182
+ * @return array
183
+
184
+ */
185
+
186
+ protected function explodeWildcardRules($results, $attribute, $rules)
187
+
188
+ {
189
+
190
+ $pattern = str_replace('*', '[^.]*', preg_quote($attribute));
191
+
192
+
193
+
194
+ $data = ValidationData::initializeAndGatherData($attribute, $this->data);
195
+ ```