Laravel form validation is a process where Laravel validates the data sent by the end-user to the application to ensure that the user sends the right data with the format needed for the application. It also ensures that the application is protected from malicious input.
A. Controller.php
$validated = $request->validate([
'title'=>'required|unique:posts|max:255',
'body'=>'required',
'password' => 'required|confirmed',
'password_confirmation' => 'required|same:password',
'file' => 'required|file|mimes:zip|size:3072'
]);
if ($validated->fails()) {
}
B. View.blade.php
@if ($errors->any()) <divclass="alert alert-danger"> <ul> @foreach ($errors->all() as$error) <li>{{$error}}</li> @endforeach </ul> </div> @endif
<input id="title" type="text" name="title" class="@error('title') is-invalid @enderror"> @error('title') <div class="alert alert-danger">{{$message}}</div> @enderror