Validate Request in Controller
Fri May 14 2021 22:59:51 GMT+0000 (Coordinated Universal Time)
Saved by
@slendabilt
/**
* Controller Code
*/
// type-hint the Request parameter so we can simply reference $request
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|unique:categories'
]);
}
/**
* View Code
*/
// I can then display error alerts in the view
@if ($errors->any())
<div>
<ul class="list-group">
@foreach ($errors->all() as $error)
<div class="alert alert-danger" role="alert">
{{ $error }}
</div>
@endforeach
</ul>
</div>
@endif
content_copyCOPY
This code allows me to validate the request to specify rules.
I can then loop through the errors and display them in the view.
https://laravel.com/docs/8.x/validation#validating-arrays
Comments