コード ```Laravel5.6で名刺管理アプリを作成しています。 フォームを作成し、名刺情報を追加登録する際に、バリデーションをかけたいのですが、うまくできません。 フォームで名刺情報の名前・会社名・電話番号・住所・emailを入力するとバリデーションが行われ、それに通ると確認画面に遷移、通らなかった場合はフォーム入力画面に戻るシステムを作っています。 しかしうまくいきません。 フォームに入力後、postがうまく確認画面に送られていないのか、と思いvar_dump関数を使い、postの中身を調べました。 postの中身は意図したものと全く異なっているものでした。 このようなエラーメッセージが続き、フォームで記入したものは送られていません。 ```ここに言語を入力 object(Illuminate\Http\Request)#55 (27) { ["json":protected]=> NULL ["convertedFiles":protected]=> NULL ["userResolver":protected]=> object(Closure)#320 (3) { ["static"]=> array(1) { ["app"]=> object(Illuminate\Foundation\Application)#5 (30) { ["basePath":protected]=> string(27) "C:\xampp\htdocs\meishikanri" ["hasBeenBootstrapped":protected]=> bool(true) ["booted":protected]=> bool(true) ["bootingCallbacks":protected]=> array(0) { } ["bootedCallbacks":protected]=> array(1) { [0]=> object(Closure)#270 (1) { ["this"]=> object(App\Providers\RouteServiceProvider)#195 (3) { ["namespace":protected]=> string(20) "App\Http\Controllers" ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } } } ["terminatingCallbacks":protected]=> array(0) { } ["serviceProviders":protected]=> array(19) { [0]=> object(Illuminate\Events\EventServiceProvider)#8 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [1]=> object(Illuminate\Log\LogServiceProvider)#11 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [2]=> object(Illuminate\Routing\RoutingServiceProvider)#14 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [3]=> object(Illuminate\Auth\AuthServiceProvider)#136 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [4]=> object(Illuminate\Cookie\CookieServiceProvider)#143 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [5]=> object(Illuminate\Database\DatabaseServiceProvider)#146 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [6]=> object(Illuminate\Encryption\EncryptionServiceProvider)#154 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [7]=> object(Illuminate\Filesystem\FilesystemServiceProvider)#157 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [8]=> object(Illuminate\Foundation\Providers\FormRequestServiceProvider)#165 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [9]=> object(Illuminate\Foundation\Providers\FoundationServiceProvider)#163 (4) { ["providers":protected]=> array(1) { [0]=> string(58) "Illuminate\Foundation\Providers\FormRequestServiceProvider" } ["instances":protected]=> array(1) { [0]=> object(Illuminate\Foundation\Providers\FormRequestServiceProvider)#165 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } } ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [10]=> object(Illuminate\Notifications\NotificationServiceProvider)#169 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [11]=> object(Illuminate\Pagination\PaginationServiceProvider)#172 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [12]=> object(Illuminate\Session\SessionServiceProvider)#177 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [13]=> object(Illuminate\View\ViewServiceProvider)#182 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [14]=> object(Fideloper\Proxy\TrustedProxyServiceProvider)#187 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [15]=> object(App\Providers\AppServiceProvider)#189 (2) { ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [16]=> object(App\Providers\AuthServiceProvider)#191 (3) { ["policies":protected]=> array(1) { ["App\Model"]=> string(24) "App\Policies\ModelPolicy" } ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [17]=> object(App\Providers\EventServiceProvider)#193 (4) { ["listen":protected]=> array(1) { ["App\Events\Event"]=> array(1) { [0]=> string(27) "App\Listeners\EventListener" } } ["subscribe":protected]=> array(0) { } ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } [18]=> object(App\Providers\RouteServiceProvider)#195 (3) { ["namespace":protected]=> string(20) "App\Http\Controllers" ["app":protected]=> *RECURSION* ["defer":protected]=> bool(false) } } ["loadedProviders":protected]=> array(19) { ["Illuminate\Events\EventServiceProvider"]=> bool(true) ["Illuminate\Log\LogServiceProvider"]=> bool(true) ["Illuminate\Routing\RoutingServiceProvider"]=> bool(true) ["Illuminate\Auth\AuthServiceProvider"]=> bool(true)
フォームのソースコードは以下の通りです。
html
1 <p>新規に登録する名刺の情報を入力してください。 2 <form action="confirm" method="post"> 3 {{ csrf_field() }} 4 <p><label for="name field" > お名前 <span class="required" > ※必須</span ></label><br> 5 <input type="text" name = "name" id = "name-field" required value="{{old('name')}}"></p > 6 <p><label for="company field" > 会社名<span class="required" > ※必須</span ></label ><br > 7 <input type="text" name = "company" id = "company-field" required value="{{old('company')}}"></p> 8 <p ><label for="tel field" > 電話番号<span class="required" > ※必須</span ></label ><br > 9 <input type="tel" name = "tel" id = "tel-field" required value="{{old('tel')}}"></p> 10 <p><label for="address field" > 住所<span class="required"> ※必須</span ></label ><br > 11 <input type="text" name = "address" id = "address-field" required value="{{old('address')}}"></p> 12 <p><label for="email field" > email<span class="required" > ※必須</span ></label ><br > 13 <input type="text" name = "hobby" id = "email-field" required value="{{old('email')}}"></p> 14 <input type = "submit" name="directupload" value="送信"> 15 </form > 16 17 <div> 18 @if(count($errors)>0) 19 <ul> 20 @foreach($errors->all() as $error) 21 <li>{{$error}}</li> 22 @endforeach 23 </ul> 24 @endif 25 26 </div> 27 </p> 28
なぜこのようなpostが送られてしまっているのか、わかりません。
ご教授いただけると幸いです。
フォームが送られる確認画面(confirm)では以下の処理を行います。
php
1 public function confirm(Request $request) 2 { 3 dd($request); 4 $validator = $this->getValidator($request); 5 6 //バリデーションチェックが通らなかったらフォーム画面にリダイレクト 7 8 if ($validator->fails()) { 9 // フォーム画面ヘリダイレクト 10 return redirect('form') 11 ->withErrors($validator) 12 ->withInput(); 13 } else { 14 15 // 確認画面を表示する*/ 16 return view('confirm'); 17 } 18 } 19
バリデーターの処理は以下の通りです。
php
1//confirm画面・result画面で使用するバリデーションのルール 2 public function getValidator(array $Input) 3 { 4 $rules = [ 5 'name' => 'required|string|max:60', 6 'company' => 'required|string|max:60', 7 'tel' => 'required|numeric|digits_between:10,11', 8 'address' => 'required|string|max:60', 9 'email' => 'required|email']; 10 11 12 $validator = Validator::make($Input, $rules); 13 14 return $validator; 15 }
フォームの名前・会社名・電話番号・住所・emailすべてを埋めた状態で送った
ddコマンドの出力は以下の通りです。
php
1Request {#55 ▼ 2 #json: null 3 #convertedFiles: null 4 #userResolver: Closure {#320 ▶} 5 #routeResolver: Closure {#343 ▶} 6 +attributes: ParameterBag {#65 ▶} 7 +request: ParameterBag {#64 ▶} 8 +query: ParameterBag {#63 ▶} 9 +server: ServerBag {#67 ▶} 10 +files: FileBag {#60 ▶} 11 +cookies: ParameterBag {#66 ▶} 12 +headers: HeaderBag {#68 ▶} 13 #content: null 14 #languages: null 15 #charsets: null 16 #encodings: null 17 #acceptableContentTypes: null 18 #pathInfo: "/confirm" 19 #requestUri: "/confirm" 20 #baseUrl: "" 21 #basePath: null 22 #method: "POST" 23 #format: null 24 #session: Store {#399 ▶} 25 #locale: null 26 #defaultLocale: "en" 27 -isHostValid: true 28 -isForwardedValid: true 29 basePath: "" 30 format: "html" 31}
回答1件
あなたの回答
tips
プレビュー