LoginSignup
juza11
@juza11

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

laravelでバリデーションを実装したいのですが、できません。

解決したいこと

laravelで、フォームリクエストを使ってバリデーションを実装したいのですが、できません。

やりたい流れは以下の通りです。
①選手一覧画面(index.blade.php)の編集ボタン押下で
②データ編集画面(update.blade.php)に移動
③データ編集画面内の編集ボタン押下でバリデーション実行。その後データ編集。

現状は③の編集ボタンを押下すると、
formタグのactionに設定したurlに飛ばしたいのですが、飛びません。
その際、エラー表示などはありません。

解決方法を教えていただきたいです。

該当するソースコード

web.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PlayersController;

Route::post('/player/henshu', [PlayersController::class, 'henshu'])->name('henshu');//データ編集
update.blade.php
<!DOCTYPE HTML>
<html>
    <head>
    <meta charset="UTF-8">
    <!-- <meta name="csrf-token" content="{{ csrf_token() }}"> -->
        <title>選手編集画面</title>
        
        <link rel="stylesheet" type="text/css" href="/css/style.css">
    </head>
    <body>
        <h1>選手編集画面</h1>
        <h2>■選手データ</h2>
        <div class="soccerplayer">
            <table id="detail">
                <form method="post" action="{{ route('henshu') }}">
                @csrf
                    <tr>
                        <th>No</th>
                        <td>{{$detail[0] -> id}}</td>
                    </tr>
                    <tr>
                        <th>背番号</th>
                        <td><input type="text" value="{{$detail[0] -> uniform_num}}"></td>
                    </tr>
                    <tr>
                        <th>ポジション</th>
                        <td><select name="position">
                            <option value="{{$detail[0] -> position}}">GK</option>
                            <option value="サンプル2">DF</option>
                            <option value="サンプル3">MF</option>
                            <option value="サンプル3">FW</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <th>名前</th>
                        <td><input type="text" value="{{$detail[0] -> name}}"></td>
                    </tr>
                    <tr>
                        <th>国</th>
                        <td><select name="country" id="selectCountry">
                            @foreach($country as $c)
                            <option value="{{$detail[0] -> country_name}}">{{$c -> name}}</option>
                            @endforeach
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <th>所属</th>
                        <td><input type="text" value="{{$detail[0] -> club}}"></td>
                    </tr>

                    <tr>
                        <th>誕生日</th>
                        <td><input type="date" name="calendar" max="9999-12-31" value="{{$detail[0] -> birth}}"></td>
                    </tr>
                    <tr>
                        <th>身長</th>
                        <td><input type="text" value="{{$detail[0] -> height}}"></td>
                    </tr>
                    <tr>
                        <th>体重</th>
                        <td><input type="text" value="{{$detail[0] -> weight}}"></td>
                    </tr>

                    <button type="submit">編集する</button>
                
                </form>
            </table>
            <div id="re">
                <!-- <a href="/player/henshu" class="edit"><button id='update_button'>編集</button></a> -->
                <a href="/" class="return"><button id='re_button'>戻る</button></a>
            </div>
        </div>
        
    </body>
</html>
PlayersController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Player;
use App\Http\Requests\PlayerRequest;

//データ編集
    public function henshu(PlayerRequest $request){
        //
        $i = 10;
        dd($i);
    }
PlayerRequest.php
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class PlayerRequest extends FormRequest
{

    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'uniform_num' => 'required',
            // 'position' => 'required'
        ];
    }
}<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class PlayerRequest extends FormRequest
{

    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'uniform_num' => 'required',
            // 'position' => 'required'
        ];
    }
}

自分で試したこと

とりあえず、route('henshu')を実行させるために、
formタグのアクションをaction="/player/henshu"
に変えて試してみましたが同じ結果でした。

0

2Answer

コードを見ただけですが,以下のように変更したらいかがでしょうか。
また,inputについて,name属性が抜けていると思います。

update.blade.php

-            <table id="detail">
                <form method="post" action="{{ route('henshu') }}">
                @csrf
+                 <table id="detail">
                    <tr>
                        <th>No</th>
                        <td>{{$detail[0] -> id}}</td>
                    </tr>
                    <tr>
                        <th>背番号</th>
-                        <td><input type="text" value="{{$detail[0] -> uniform_num}}"></td>
+                        <td><input type="text" name="uniform_num" value="{{$detail[0] -> uniform_num}}"></td>
                    </tr>
                    <tr>
                        <th>ポジション</th>
                        <td><select name="position">
                            <option value="{{$detail[0] -> position}}">GK</option>
                            <option value="サンプル2">DF</option>
                            <option value="サンプル3">MF</option>
                            <option value="サンプル3">FW</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <th>名前</th>
                        <td><input type="text" value="{{$detail[0] -> name}}"></td>
                    </tr>
                    <tr>
                        <th></th>
                        <td><select name="country" id="selectCountry">
                            @foreach($country as $c)
                            <option value="{{$detail[0] -> country_name}}">{{$c -> name}}</option>
                            @endforeach
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <th>所属</th>
                        <td><input type="text" value="{{$detail[0] -> club}}"></td>
                    </tr>

                    <tr>
                        <th>誕生日</th>
                        <td><input type="date" name="calendar" max="9999-12-31" value="{{$detail[0] -> birth}}"></td>
                    </tr>
                    <tr>
                        <th>身長</th>
                        <td><input type="text" value="{{$detail[0] -> height}}"></td>
                    </tr>
                    <tr>
                        <th>体重</th>
                        <td><input type="text" value="{{$detail[0] -> weight}}"></td>
                    </tr>

                    <button type="submit">編集する</button>
+                 </table>                
                </form>
-            </table>
1Like

Comments

  1. @juza11

    Questioner

    返信ありがとうございます。
    アドバイスの通り修正したところ、player/henshuに飛ぶことはできたのですが、
    画像のようなエラーが出てしまいました。
    ・エラー表示されているファイルが、この操作では関係ないはずのindex.blade.phpなのがなぜなのか
    ・画像右側のエラー(Internal Server Error)の原因がわかりませんでした。
    これらについて、もし考えられる原因等ありましたら教えていただけると幸いです。スクリーンショット 2023-12-02 18.39.04.png

  2. エラーの意味はわかりますか?
    また、どのタイミングでエラーが出るのかがわかりません。

アドバイスの通り修正したところ、player/henshuに飛ぶことはできたのですが、
画像のようなエラーが出てしまいました。
・エラー表示されているファイルが、この操作では関係ないはずのindex.blade.phpなのがなぜなのか

コマンドでルート設定を確認してください。
POST /player/henshuは想定しているコントローラーに割り当てられているでしょうか?設定が重複するなどして想定外の割当がされる例はあります。

php artisan route:list

1Like

Your answer might help someone💌