doc: Laravel: validation
Laravel 提供valide方法讓我們可以驗證request
$validatedData = $request->validate([
'title' => ['required', 'unique:posts', 'max:255'],
'body' => ['required'],
]);
這邊有所有validate提供的rule: Laravel: validate
而當我們fail validation, Laravel也會幫我們蒐集error
也可以透過ShareErrorsFromSession Middleware,使用$errors
變數。
在blade裡面印出error來:
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif