質問編集履歴
2
更新
title
CHANGED
File without changes
|
body
CHANGED
File without changes
|
1
更新
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,229 +1,225 @@
|
|
1
|
+
お疲れ様です。先日のエラーですがミニプログラムを作成したので、添付します。
|
2
|
+
|
1
|
-
|
3
|
+
オリジナルのバリデートを作成しているクラス
|
2
4
|
```php
|
5
|
+
<?php
|
6
|
+
|
7
|
+
namespace App\Providers;
|
8
|
+
|
9
|
+
use Illuminate\Support\ServiceProvider;
|
10
|
+
use Illuminate\Support\Facades\Validator;
|
11
|
+
|
12
|
+
class Test_ValidationServiceProvider extends ServiceProvider
|
13
|
+
{
|
14
|
+
/**
|
15
|
+
* Register services.
|
16
|
+
*
|
17
|
+
* @return void
|
18
|
+
*/
|
19
|
+
public function register()
|
20
|
+
{
|
21
|
+
//
|
22
|
+
}
|
23
|
+
|
24
|
+
/**
|
25
|
+
* Bootstrap services.
|
26
|
+
*
|
27
|
+
* @return void
|
28
|
+
*/
|
29
|
+
public function boot()
|
30
|
+
{
|
31
|
+
|
32
|
+
//特殊文字は許さない
|
33
|
+
/*以下の条件に一致しない(特殊文字が含まれている)ときにエラー処理を行う*/
|
34
|
+
Validator::extend('invalid', function ($attribute, $value, $parameters, $validator) {
|
3
|
-
|
35
|
+
$pattern = '-._~%:/?#[]@!$&\'()*+,;=';
|
36
|
+
$pattern = preg_quote($pattern, '/');
|
37
|
+
$pattern = "/[^" . $pattern . "]/";
|
38
|
+
return preg_match($pattern, $value);
|
39
|
+
});
|
40
|
+
|
41
|
+
//IDとパスワードはパスワードは英数字を含んだ16文字以下
|
42
|
+
Validator::extend('passid', function ($attribute, $value, $parameters, $validator) {
|
43
|
+
return preg_match("/\A(?=.*?[a-z])(?=.*?\d)[a-z\d]{5,16}+\z/i", $value);
|
44
|
+
});
|
45
|
+
}
|
46
|
+
}
|
4
47
|
```
|
48
|
+
ルートです
|
49
|
+
```php
|
50
|
+
//テンプレであるコメントアウトは提示していません。
|
51
|
+
Route::get('/', 'TestController@basic1');
|
52
|
+
Route::post('/basic1', 'TestController@basic1Check');
|
53
|
+
Route::post('/basic2', 'TestController@basic2');
|
54
|
+
```
|
55
|
+
オリジナルのバリデートをルールに追加しています
|
56
|
+
```php
|
57
|
+
<?php
|
58
|
+
namespace App\Http\Requests;
|
59
|
+
use Illuminate\Foundation\Http\FormRequest;
|
5
60
|
|
61
|
+
class TestRequest extends FormRequest
|
62
|
+
{
|
63
|
+
/**
|
64
|
+
* Determine if the user is authorized to make this request.
|
65
|
+
*
|
66
|
+
* @return bool
|
67
|
+
*/
|
68
|
+
public function authorize()
|
69
|
+
{
|
70
|
+
return true;
|
71
|
+
}
|
72
|
+
|
73
|
+
/**
|
74
|
+
* Get the validation rules that apply to the request.
|
75
|
+
*
|
76
|
+
* @return array
|
77
|
+
*/
|
78
|
+
public function rules()
|
79
|
+
{
|
80
|
+
return [
|
81
|
+
'name' => 'required|invalid|',
|
82
|
+
'id' => 'passid',
|
83
|
+
'pass' => 'passid|different:id',
|
84
|
+
];
|
85
|
+
}
|
86
|
+
|
87
|
+
public function messages()
|
88
|
+
{
|
89
|
+
return [
|
6
|
-
|
90
|
+
//名前のバリデーション
|
91
|
+
'name.required' => '名前の入力は必須です',
|
92
|
+
'name.invalid' => '名前に使用できない文字が含まれています',
|
93
|
+
//idのバリデーション
|
94
|
+
'id.passid' => 'ログインIDは英数字を含んだ16文字以下で入力してください',
|
95
|
+
//passのバリデーション
|
96
|
+
'pass.passid' => 'パスワードは英数字を含んだ16文字以下で入力してください',
|
97
|
+
'pass.different' => 'IDと同じパスワードは設定できません',
|
98
|
+
];
|
99
|
+
}
|
100
|
+
}
|
101
|
+
```
|
102
|
+
コントローラーです
|
7
103
|
```php
|
104
|
+
<?php
|
105
|
+
|
106
|
+
namespace App\Http\Controllers;
|
107
|
+
|
108
|
+
use Illuminate\Http\Request;
|
109
|
+
use App\Http\Requests\TestRequest;
|
110
|
+
|
111
|
+
class TestController extends Controller
|
112
|
+
{
|
113
|
+
//一番初めに呼ばれます
|
8
114
|
public function basic1()
|
9
115
|
{
|
10
116
|
$hash = array(
|
11
117
|
'name' => null,
|
12
|
-
'kana' => null,
|
13
|
-
'name' => null,
|
14
|
-
'sex' => null,
|
15
|
-
'tel' => null,
|
16
|
-
'email' => null,
|
17
118
|
'id' => null,
|
18
119
|
'pass' => null,
|
19
120
|
);
|
20
|
-
return view('
|
121
|
+
return view('tests.test_basic1')->with($hash);
|
21
122
|
}
|
22
|
-
```
|
23
|
-
|
24
|
-
表示先のビューで項目を入力し、ボタンを押しそれを受けるルート
|
25
|
-
```php
|
26
|
-
|
123
|
+
//test_basic1から送信された値がバリデートを通過したあとの処理です
|
27
|
-
```
|
28
|
-
そのルートに呼ばれるアクション(自作のバリデートクラスを使用)。
|
29
|
-
入力項目を操作し、次のページへ。
|
30
|
-
```php
|
31
|
-
public function basic1Check(
|
124
|
+
public function basic1Check(TestRequest $request)
|
32
125
|
{
|
33
126
|
$name = $request->name;
|
34
|
-
$kana = $request->kana;
|
35
|
-
$sex = $request->sex;
|
36
|
-
$tel = $request->tel;
|
37
|
-
$email = $request->email;
|
38
127
|
$id = $request->id;
|
39
128
|
$pass = $request->pass;
|
40
|
-
//値を配列に格納します
|
129
|
+
//値を配列に格納し、分解します
|
41
|
-
$basic1 = ['name' => $name, '
|
130
|
+
$basic1 = ['name' => $name, 'id' => $id, 'pass' => $pass];
|
131
|
+
//この値がtest_basic2のbuttonタグのvalueに入ります
|
42
132
|
$basic1Str = implode(',', $basic1);
|
43
|
-
return view('
|
133
|
+
return view('tests.test_basic2', ['basic1Str' => $basic1Str]);
|
44
134
|
}
|
45
|
-
```
|
46
135
|
|
136
|
+
public function basic2(Request $request)
|
137
|
+
{
|
138
|
+
//このifでbackボタンが押されたかどうか判断していま
|
139
|
+
if (isset($request->basic1Str)) {
|
140
|
+
$basic1 = explode(',', $request->basic1Str);
|
141
|
+
$name = $basic1[0];
|
47
|
-
|
142
|
+
$id = $basic1[1];
|
143
|
+
$pass = $basic1[2];
|
48
144
|
|
49
|
-
**問題**
|
50
|
-
コード非常に長いので、全て載せることが出来ないのですが、2ページ目で戻るボタンを押し1ページ目に遷移するときちんと入力した値が保持されています。
|
51
|
-
|
52
|
-
また値を変更し2ページ目に遷移し、再び1ページ目に遷移しても、きちんと直近の値がvalueに残っています。
|
53
|
-
|
54
|
-
問題は2ページ目から1ページ目に戻り、エラー値を入力した場合のみ以下のエラーが出ます。
|
55
|
-
1番初めに1ページ目にきて、エラー値を入力すると、きちんと自作クラスで定めたエラーメッセージが
|
56
|
-
画面に表示されます。
|
57
|
-
|
145
|
+
$hash = array(
|
146
|
+
'name' => $name,
|
147
|
+
'id' => $id,
|
148
|
+
'pass' => $pass,
|
149
|
+
);
|
58
|
-
|
150
|
+
return view('tests.test_basic1')->with($hash);
|
151
|
+
}
|
152
|
+
return view('tests.test_basic2');
|
153
|
+
}
|
154
|
+
}
|
59
155
|
```
|
60
|
-
1回目と遷移後のバリデーションチェックの飛び先が違うのでしょうか。
|
61
|
-
|
156
|
+
以下bladeです
|
62
|
-
basic1.blade.phpとbasic2.blade.phpを貼っておきます
|
63
|
-
```
|
157
|
+
```blade
|
64
|
-
@extends('layouts.layout')
|
65
|
-
|
66
|
-
@section('title', 'basic1')
|
67
|
-
|
68
|
-
|
158
|
+
{{--一部抜粋です(test_basic1.php)--}}
|
69
|
-
|
70
|
-
|
71
|
-
<form method="post" action="/basic1">
|
159
|
+
<form method="post" action="/basic1">
|
72
|
-
|
73
|
-
|
160
|
+
{{ csrf_field() }}
|
74
|
-
|
75
|
-
|
161
|
+
<p>ナマエ</p>
|
76
|
-
<label for="formGroupExampleInput" class="font-weight-bold">名前</label>
|
77
|
-
<input type="text" name="name" class="form-control" id="formGroupExampleInput" placeholder="名前をスペースなしで入力してください"
|
78
|
-
|
162
|
+
<input type="text" name="name" value="{{ old('name', $name) }}">
|
79
163
|
@if ($errors->has('name'))
|
80
164
|
<span class="errormsg">{{ $errors->first('name') }}</span>
|
81
165
|
@endif
|
82
|
-
</div>
|
83
166
|
|
84
|
-
<div class="form-group mb-5">
|
85
|
-
<label for="formGroupExampleInput" class="font-weight-bold">ナマエ</label>
|
86
|
-
<input type="text" name="kana" class="form-control" id="formGroupExampleInput" placeholder="ナマエをスペースなしで入力してください"
|
87
|
-
maxlength="64" value="{{ old('kana', $kana) }}">
|
88
|
-
@if ($errors->has('kana'))
|
89
|
-
<span class="errormsg">{{ $errors->first('kana') }}</span>
|
90
|
-
@endif
|
91
|
-
</div>
|
92
167
|
|
93
|
-
<p class="font-weight-bold h6">性別</p>
|
94
|
-
<div class="custom-control custom-radio custom-control-inline mb-5">
|
95
|
-
<input type="radio" id="customRadioInline1" name="sex" class="custom-control-input" value="male" @if (old('sex',
|
96
|
-
$sex)=="male" ) checked @endif>
|
97
|
-
<label class="custom-control-label font-weight-bold" for="customRadioInline1">男性</label>
|
98
|
-
|
168
|
+
<p>ログインID</p>
|
99
|
-
<div class="custom-control custom-radio custom-control-inline">
|
100
|
-
<input type="radio" id="customRadioInline2" name="sex" class="custom-control-input" value="female"
|
101
|
-
@if(old('sex', $sex)=="female" ) checked @endif>
|
102
|
-
<label class="custom-control-label font-weight-bold" for="customRadioInline2">女性</label>
|
103
|
-
</div>
|
104
|
-
@if ($errors->has('sex'))
|
105
|
-
<span class="errormsg">{{ $errors->first('sex') }}</span>
|
106
|
-
@endif
|
107
|
-
|
108
|
-
<div class="form-group mb-5">
|
109
|
-
<label for="formGroupExampleInput" class="font-weight-bold">電話番号</label>
|
110
|
-
<input type="tel" name="tel" class="form-control" id="formGroupExampleInput" placeholder="電話番号をハイフンなしで入力してください"
|
111
|
-
value="{{ old('tel', $tel) }}">
|
112
|
-
@if ($errors->has('tel'))
|
113
|
-
<span class="errormsg">{{ $errors->first('tel') }}</span>
|
114
|
-
@endif
|
115
|
-
</div>
|
116
|
-
|
117
|
-
<div class="form-group mb-5">
|
118
|
-
<label for="formGroupExampleInput" class="font-weight-bold">メールアドレス(任意)</label>
|
119
|
-
<input type="text" name="email" class="form-control" id="formGroupExampleInput" placeholder="メールアドレスを入力してください"
|
120
|
-
value="{{ old('email', $email) }}">
|
121
|
-
@if ($errors->has('email'))
|
122
|
-
<span class="errormsg">{{ $errors->first('email') }}</span>
|
123
|
-
@endif
|
124
|
-
</div>
|
125
|
-
|
126
|
-
<div class="form-group mb-5">
|
127
|
-
<label for="formGroupExampleInput" class="font-weight-bold">ログインID</label>
|
128
|
-
<input type="text" name="id" class="form-control" id="formGroupExampleInput"
|
129
|
-
|
169
|
+
<input type="text" name="id" value="{{ old('id', $id) }}">
|
130
170
|
@if ($errors->has('id'))
|
131
171
|
<span class="errormsg">{{ $errors->first('id') }}</span>
|
132
172
|
@endif
|
133
|
-
</div>
|
134
173
|
|
174
|
+
|
135
|
-
|
175
|
+
<p>パスワード</p>
|
136
|
-
<label for="formGroupExampleInput" class="font-weight-bold">パスワード</label>
|
137
|
-
<input type="text" name="pass"
|
176
|
+
<input type="text" name="pass" value="{{ old('pass', $pass) }}">
|
138
|
-
placeholder="パスワードは英数字を含んだ16文字以下で入力してください" maxlength="16">
|
139
177
|
@if ($errors->has('pass'))
|
140
178
|
<span class="errormsg">{{ $errors->first('pass') }}</span>
|
141
179
|
@endif
|
142
|
-
</div>
|
143
180
|
|
144
|
-
|
181
|
+
<button type="submit" class="btn btn-primary mb-5">Next</button>
|
145
|
-
|
146
|
-
</form>
|
182
|
+
</form>
|
147
|
-
@endsection
|
148
183
|
```
|
149
184
|
|
150
|
-
```
|
185
|
+
```blade
|
151
|
-
|
186
|
+
{{--一部抜粋です(test_basic2.php)--}}
|
187
|
+
<form method="post" action="/basic2">
|
188
|
+
{{ csrf_field() }}
|
152
189
|
|
153
|
-
@section('title', 'basic2')
|
154
|
-
|
155
|
-
@section('content')
|
156
|
-
|
157
|
-
<form action="/basic2" method="post">
|
158
|
-
|
159
|
-
{{ csrf_field() }}
|
160
|
-
|
161
|
-
<div class="form-row mt-5">
|
162
|
-
<div class="form-group col-md-6">
|
163
|
-
<label for="inputZip" class="font-weight-bold">郵便番号</label>
|
164
|
-
<input id="inputZip" class="form-control" type="text">
|
165
|
-
</div>
|
166
|
-
<div class="form-group col-md-6">
|
167
|
-
<label for="inputState" class="font-weight-bold">都道府県</label>
|
168
|
-
<select id="inputState" class="form-control">
|
169
|
-
<option selected>Choose...</option>
|
170
|
-
<option>...</option>
|
171
|
-
</select>
|
172
|
-
</div>
|
173
|
-
</div>
|
174
|
-
|
175
|
-
<div class="form-group my-5">
|
176
|
-
<label for="inputAddress" class="font-weight-bold">以降の住所</label>
|
177
|
-
<input id="inputAddress" class="form-control" type="text" placeholder="1234 Main St">
|
178
|
-
</div>
|
179
|
-
|
180
|
-
<div class="form-group mb-5">
|
181
|
-
<label for="inputReason" class="font-weight-bold">知ったきっかけ</label>
|
182
190
|
<select id="inputReason" class="custom-select">
|
183
191
|
<option selected>Open this select menu</option>
|
184
192
|
<option value="1">One</option>
|
185
193
|
<option value="2">Two</option>
|
186
194
|
<option value="3">Three</option>
|
187
195
|
</select>
|
196
|
+
<button type="submit">Next</button>
|
197
|
+
<button type="submit" name="basic1Str" value="{{ $basic1Str }}">back</button>
|
188
|
-
</
|
198
|
+
</form>
|
199
|
+
```
|
200
|
+
**問題**
|
201
|
+
①一番初めにurlに'/'のurlを打つ
|
189
202
|
|
190
|
-
<div class="form-group mb-5">
|
191
|
-
<label for="inputReason" class="font-weight-bold">目的</label>
|
192
|
-
<select id="inputReason" class="custom-select">
|
193
|
-
<option selected>Open this select menu</option>
|
194
|
-
<option value="1">One</option>
|
195
|
-
<option value="2">Two</option>
|
196
|
-
<option value="3">Three</option>
|
197
|
-
|
203
|
+
②test_basic1.blade.php表示
|
198
|
-
</div>
|
199
204
|
|
205
|
+
③
|
200
|
-
|
206
|
+
test_basic1.balde.phpでバリデートを通過する値を入力(通過しない値でもきちんと画面に設定したエラーメッセージがでる。)
|
201
|
-
<div class="form-group mb-5">
|
202
|
-
<div class="custom-control custom-switch custom-control-inline">
|
203
|
-
<input id="customSwitch1" class="custom-control-input" type="radio" name="toeic" value="have">
|
204
|
-
<label class="custom-control-label font-weight-bold" for="customSwitch1">受験経験あり</label>
|
205
|
-
</div>
|
206
|
-
<div class="custom-control custom-switch custom-control-inline">
|
207
|
-
<input id="customSwitch2" class="custom-control-input" type="radio" name="toeic" value="have_not" checked>
|
208
|
-
<label class="custom-control-label font-weight-bold" for="customSwitch2">受験経験なし</label>
|
209
|
-
</div>
|
210
|
-
</div>
|
211
207
|
|
212
|
-
<div class="form-group mb-5">
|
213
|
-
|
208
|
+
④通過するとtest_basic2.blade.phpを表示(backボタンのvalueにはtest_basic1で入力した値がきちんと入っていることは確認済みです)
|
214
|
-
<input id="inputResult" class="form-control score" type="text" name="score" value="" placeholder="点数を入力してください">
|
215
|
-
</div>
|
216
209
|
|
217
|
-
<div class="form-group mb-5">
|
218
|
-
<button type="submit" class="btn btn-primary mr-5">Next</button>
|
219
|
-
<button type="submit" class="btn btn-secondary" id="" name="basic1Str" value="{{ $basic1Str }}">back</button>
|
220
|
-
|
210
|
+
⑤backボタンを押す
|
221
211
|
|
222
|
-
|
212
|
+
⑥test_basic1.blade.phpが表示され、先ほど入力した値がすでに入力済み(期待値)
|
223
|
-
@endsection
|
224
213
|
|
214
|
+
⑦-1
|
215
|
+
バリデート通過する値を入力すると問題なくtest_basic2.blade.phpが表示される
|
216
|
+
⑦-2
|
225
|
-
|
217
|
+
バリデート通過しない値を入力すると以下のエラーが表示される
|
218
|
+
```error
|
226
|
-
|
219
|
+
The GET method is not supported for this route. Supported methods: POST.
|
227
|
-
|
220
|
+
```
|
228
221
|
|
222
|
+
一番初めにtest_basic1.blade.phpでバリデートエラーを出してもtest_basic2.blade.phpからtest_basic1.blade.phpに戻ってきてバリデートエラーを出しても動きは同じだと思うのですが、何が問題でしょうか。
|
223
|
+
|
229
|
-
|
224
|
+
またバリデートクラスの作成は以下のサイトの方法と全く同じです。
|
225
|
+
[Laravelのカスタムバリデーション](https://minory.org/laravel-custom-validation-rule.html)
|