バージョン Laravel Framework 6.5.0
#問題点
フォームリクエストを使用してバリデーションを適用させようとした時に/Signupから/Homeへ画面遷移出来ない。
#試したこと
csrf_fieldの適用
以下コード
CreateUserRequest.php
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class CreateUserRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * パスワード7文字以上 * パスワード(確認用)の入力チェック * 全ての項目が入力されているか * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'username' => 'required|max:20', 'email' => 'required|email|max:100', 'password' => 'required|min:7|confirmed', // ]; } }
UserController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests\CreateUserRequest; class UserController extends Controller { public function Login() { return view('Lessons.Login'); } public function Signup() { return view('Lessons.Signup'); } public function Home(Request $request) { $name = $request->input('name'); return view('Lessons.Home', compact('name')); } public function postCreate(CreateUserRequest $req) { /** * 拡張クラスに書いたルールでリクエストが自動的に検証される * バリデーションをパスするとこの後の処理が実行される */ $this->userService->createUser($req->all()); return view('Lessons.Home'); } }
web.php
<?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () { return view('welcome'); }); Route::get('Login', 'UserController@Login')->name('Login'); Route::get('Signup', 'UserController@Signup')->name('Signup'); Route::get('Home', 'UserController@Home')->name('Home'); Route::group(['middleware' => ['web']], function () { Route::post('Home', 'UserController@postCreate'); });
Home.blade.php
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>home</title> <link rel="stylesheet" type="text/css" href="login.css" media="all" /> <style> .top-username { position: absolute; right: 120px; top: 18px; color: lightcoral; text-decoration: none; } .top-logout { position: absolute; right: 30px; top: 18px; color: lightcoral; text-decoration: none; } #form { background: #053352; background-image: -webkit-linear-gradient(top, #053352, Courier New); background-image: -moz-linear-gradient(top, #053352, Courier New); background-image: -ms-linear-gradient(top, #053352, Courier New); background-image: -o-linear-gradient(top, #053352, Courier New); background-image: linear-gradient(to bottom, #053352, Courier New); -webkit-border-radius: 6; -moz-border-radius: 6; border-radius: 6px; font-family: Courier New; color: #ffffff; font-size: 30px; padding: 20px 20px 20px 20px; text-decoration: none; text-align: center; } </style> </head> <body> <p><a class="top-username" href={{ route('Home')}}>username</a></p> <p><a class="top-logout" href={{ route('Login')}}>ログアウト</a></p> <br> <br> <br> <br> <br> <div id="form"> <form method="POST" action="/Home"> {{ csrf_field() }} <p>Welcome!</p> <p>Your Home Page!</p> </form> </div> </body> </html>
Signup Blade.php
@extends('layouts.Loginapp') @section('title') 新規登録 @endsection @section('form') <p>名前</p> <p class="name"><input type="text" name="name" maxlength="32" autocomplete="OFF" /></p> <p>メールアドレス</p> <p class="mailaddress"><input type="text" name="mailaddress" maxlength="32" autocomplete="OFF" /></p> <p>パスワード</p> <p class="password"><input type="password" name="password" maxlength="32" autocomplete="OFF" /></p> <p>パスワード(確認用)</p> <p class="Confirm password"><input type="password" name="password_confirmation" maxlength="32" autocomplete="OFF" /></p> <p class="submit"><input type="submit" value="登録" class="top-username" href={{ route('Home')}}></p> @endsection
Loginapp.blade.php
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>@yield('title')</title> <link rel="stylesheet" type="text/css" href="login.css" media="all" /> <style> body, p, form, input { margin: 0 } .top-login { position: absolute; right: 120px; top: 18px; color: lightcoral; text-decoration: none; } .top-signup { position: absolute; right: 30px; top: 18px; color: lightcoral; text-decoration: none; } #form { width: 350px; margin: 30px auto; padding: 20px; border: 1px solid #555; } form p { font-size: 14px; } .form-title { font-size: 20px; text-align: center; margin-bottom: 20px; border-bottom: solid 3px #fff; " } .name { margin-bottom: 20px; } .mailaddress { margin-bottom: 20px; } .password { margin-bottom: 20px; } .confirm password { margin-bottom: 20px; } input[type="text"], input[type="password"] { width: 350px; padding: 4px; font-size: 14px; margin: 0 auto; } .submit { text-align: center; } /* skin */ #form { background: #053352; background-image: -webkit-linear-gradient(top, #053352, Courier New); background-image: -moz-linear-gradient(top, #053352, Courier New); background-image: -ms-linear-gradient(top, #053352, Courier New); background-image: -o-linear-gradient(top, #053352, Courier New); background-image: linear-gradient(to bottom, #053352, Courier New); -webkit-border-radius: 6; -moz-border-radius: 6; border-radius: 6px; font-family: Courier New; color: #ffffff; font-size: 20px; padding: 20px 20px 20px 20px; text-decoration: none; } .submit input { background: #f78181; background-image: -webkit-linear-gradient(top, #f78181, #f78181); background-image: -moz-linear-gradient(top, #f78181, #f78181); background-image: -ms-linear-gradient(top, #f78181, #f78181); background-image: -o-linear-gradient(top, #f78181, #f78181); background-image: linear-gradient(to bottom, #f78181, #f78181); -webkit-border-radius: 8; -moz-border-radius: 8; border-radius: 8px; -webkit-box-shadow: 1px 1px 3px #666666; -moz-box-shadow: 1px 1px 3px #666666; box-shadow: 1px 1px 3px #666666; font-family: Courier New; color: #ffffff; font-size: 16px; padding: 10px 20px 10px 20px; text-decoration: none; } .submit input:hover { background: #f5c7c7; background-image: -webkit-linear-gradient(top, #f5c7c7, #f5c7c7); background-image: -moz-linear-gradient(top, #f5c7c7, #f5c7c7); background-image: -ms-linear-gradient(top, #f5c7c7, #f5c7c7); background-image: -o-linear-gradient(top, #f5c7c7, #f5c7c7); background-image: linear-gradient(to bottom, #f5c7c7, #f5c7c7); text-decoration: none; } #form { background: #053352; background-image: -webkit-linear-gradient(top, #053352, Courier New); background-image: -moz-linear-gradient(top, #053352, Courier New); background-image: -ms-linear-gradient(top, #053352, Courier New); background-image: -o-linear-gradient(top, #053352, Courier New); background-image: linear-gradient(to bottom, #053352, Courier New); -webkit-border-radius: 6; -moz-border-radius: 6; border-radius: 6px; font-family: Courier New; color: #ffffff; font-size: 20px; padding: 20px 20px 20px 20px; text-decoration: none; } </style> </head> <body> <p><a class="top-login" href={{ route('Login')}}>ログイン</a></p> <p><a class="top-signup" href={{ route('Signup')}}>新規登録</a></p> <br> <br> <br> <br> <br> <br> <div id="form"> <p class=form-title>@yield('title')</p> <form method="POST" action="/Home"> {{ csrf_field() }} @yield('form') </form> </div> </body> </html>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。