LoginSignup
0
0

More than 1 year has passed since last update.

【Laravel】ラジオボタンやプルダウンでグルーピングされている値のバリデーション方法

Last updated at Posted at 2022-07-13

Laravelのバリデーション機能を実装した際、ラジオボタンやプルダウンでグルーピングされている値に関するバリデーションの情報がパッと見つからなかったため、備忘録として記載いたします。
当然ながらLaravelの公式に書いてあるのですが、初見では全く見つけられませんでした。
結論だけご覧いただいてご理解いただけた方は、結論をご参照の上、実装を進めてみてください。

動作環境

  • Laravel:Laravel8.83.12
  • PHP:8.1.5

結論

  • Rule::in(["◯" , "△" , "□"])を用いる方法
  • ["in:◯,△,□,"]を用いる方法
    があります。

Request

app/Http/Requests/TestRequest.php
    public function rules() {
        return [
            //Rule::in([])を用いる方法
            "fruits1" => Rule::in(["apple", "orange", "peach", "grape"]),
            //["in:"]を用いる方法
            "fruits2" =>["in:apple,orange,peach,grape"],
        ];
    }

["in:◯,△,□,"]を用いる場合、半角スペースは入れないようにしてください。

これでラジオボタンやプルダウンで用意されている内容を選択しなかった場合、バリデーションエラーが走ります。
以下、 動作確認環境の構築です。

ルート設定

routes/web.php
<?php
use App\Http\Controllers\TestController;
use Illuminate\Support\Facades\Route;

Route::get('/test' , [TestController::class , 'index'])->name('test.index');
Route::post('/test' , [TestController::class , 'search'])->name('test.search');

Controller

app/Http/Controllers/TestController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Contracts\Support\Renderable;
use App\Http\Requests\TestRequest;

class TestController extends Controller {
    /**
     * testのviewを返却します。
     *
     * @return Renderable
     */
    public function index(): Renderable {
        return view('test.index');
    }

    /**
     * バリデーションを実行結果をdd()で表示します。
     *
     * @param  TestRequest $request
     * @return Renderable
     */
    public function search(TestRequest $request): Renderable {
        $input = $request->only(['fruits1', 'fruits2']);
        dd($input);
    }
}


View

index.blade.php
<form action="{{route('test.search')}}" method='post'>
    @csrf
    <input type="radio" name="fruits1" value="apple" id="apple" checked>
    <label for="apple">
        りんご
    </label>
    <input type="radio" name="fruits1" value="orange" id="orange">
    <label for="orange">
        みかん
    </label>
    <input type="radio" name="fruits1" value="peach" id="peach">
    <label for="peach">
        もも
    </label>
    <input type="radio" name="fruits1" value="grape" id="grape">
    <label for="grape">
        ぶどう
    </label>
    <input type="radio" name="fruits1" value="pear" id="pear">
    <label for="pear">
        なし
    </label>
    <select name="fruits2" id="fruits2">
            <option value="apple" selected>りんご</option>
            <option value="orange">みかん</option>
            <option value="peach">もも</option>
            <option value="grape">ぶどう</option>
            <option value="pear">なし</option>
    </select>
    <button type="submit" value="send">送信</button>
</form>

{{-- バリデーションエラーの表示先 --}}
@if($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

ここでは、バリデーションエラーが発生する内容として<input type="radio" name="fruits1" value="pear" id="pear"><option value="pear">なし</option>を用意しました。
これらを選択して送信ボタンを押すと、バリデーションエラーが発生します。
それ以外の果物を選択した場合、バリデーションを通過してdd()の結果が表示されます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0