Laravel 驗證表單方法

內置的方法

    public function store(Request $request) {
        $messages = [
            'name.required' => '請輸入帳號名稱',
            'name.unique' => '帳號名稱已被使用',
            'name.min' => '帳號名稱至少4個字',
            'name.regex' => '帳號名稱只能是英文或數字',
            'email.required' => '請輸入電郵',
            'email.email' => '請輸入正確的電郵格式',
            'email.unique' => '電郵已被使用',
            'password.required' => '請輸入密碼',
            'password.min' => '密碼至少8個字',
            'password.confirmed' => '兩次密碼不相同',
            'password.regex' => '密碼必須至少包含一個字母',
            'password_confirmation.required' => '請再次輸入相同密碼',
        ];
        $validated = $request->validate([
            'name' => ['required', 'unique:users', 'min:4', 'regex:/^[a-zA-Z0-9]+$/u'],
            'email' => 'required|email|unique:users',
            'password' => ['required', 'confirmed', 'min:8', 'regex:/[a-zA-Z]/'],
            'password_confirmation' => 'required',
        ], $messages);
    }

把驗證步驟拿出來

use App\Http\Requests\RegisterRequest;

... 

    public function store(RegisterRequest $request) {

        $validatedData = $request->validated();

        $user = User::create([
            'name' => $validatedData['name'],
            'email' => $validatedData['email'],
            'password' => Hash::make($validatedData['password']),
        ]);

        Auth::login($user);

        return redirect(route("home"))->with('success', '成功註冊');

    }

app/Http/Requests/RegisterRequest.php

<?php
namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class RegisterRequest extends FormRequest {
    public function authorize() {
        return true;
    }

    public function rules() {
        return [
            'name' => ['required', 'unique:users', 'min:4', 'regex:/^[a-zA-Z0-9]+$/u'],
            'email' => 'required|email|unique:users',
            'password' => ['required', 'confirmed', 'min:8', 'regex:/[a-zA-Z]/'],
            'password_confirmation' => 'required',
        ];
    }

    public function messages() {
        return [
            'name.required' => '請輸入帳號名稱',
            'name.unique' => '帳號名稱已被使用',
            'name.min' => '帳號名稱至少4個字',
            'name.regex' => '帳號名稱只能是英文或數字',
            'email.required' => '請輸入電郵',
            'email.email' => '請輸入正確的電郵格式',
            'email.unique' => '電郵已被使用',
            'password.required' => '請輸入密碼',
            'password.min' => '密碼至少8個字',
            'password.confirmed' => '兩次密碼不相同',
            'password.regex' => '密碼必須至少包含一個字母',
            'password_confirmation.required' => '請再次輸入相同密碼',
        ];
    }
}

Blade 顯示,一次顯示所有error

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

個別位置顯示


<div class="fv-row mb-8">
    <div class="input-group">
        <span class="input-group-text" id="basic-addon1">@</span>
        <input type="text" class="form-control bg-transparent" placeholder="帳號名稱" name="name" value="{{old('name')}}" />
    </div>
    @error('name')
    <div class="alert alert-danger mt-2 py-2 px-4">{{ $message }}</div>
    @enderror
</div>