In this post, I am going to cover A to Z about Laravel Validation.
Things we will talk about in this post.
After Installing Laravel we will create a sample form to see our validation.
1 - Create Form
To create form in resources/views
folder create a view file form.blade.php
.
@extends('app')
@section('content')
<!-- Container (Contact Section) -->
<div id="contact" class="container">
<h3 class="text-center">Create User</h3>
<p class="text-center"><em>Register Here</em></p>
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-sm-6 form-group">
<input class="form-control" id="name" name="name" placeholder="Full Name" type="text" required>
</div>
<div class="col-sm-6 form-group">
<input class="form-control" id="email" name="email" placeholder="Email" type="email" required>
</div>
</div>
<div class="row">
<div class="col-sm-6 form-group">
<input class="form-control" id="gender" name="gender" placeholder="Gender" type="text" required>
</div>
<div class="col-sm-6 form-group">
<input class="form-control" id="password" name="password" placeholder="Password" type="password" required>
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
<button class="btn pull-right" type="submit">Send</button>
</div>
</div>
</div>
</div>
</div>
@endsection
2 - Create Validation
To create validation Rule write inside controller
$request->validate([
'name' => 'required',
'email' => 'required',
'gender' => 'required',
'password' => 'required',
]);
To Display the error message on Input Fields
@error('name')
<span class="text-danger">{{$message}}</span>
@enderror
3 - Create Validation Using Validator Facade
To create validation using Validator Facade we can write in controller
$validate = Validator::make($request->all(), [
'name' => 'required|min:5',
'email' => 'required',
'gender' => 'required',
'password' => 'required',
],[
'name.required' => 'Name is must.',
'name.min' => 'Name must have 5 char.',
]);
if($validate->fails()){
return back()->withErrors($validate->errors())->withInput();
}
4 - Create Request File For Validation
To Create a Request File we have to run a command
php artisan make:request FormDataRequest
This will create a file inside App\Http\Requests
folder.
The file will be like.
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class FormDataRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
'name' => 'required|min:5',
'email' => 'required',
'gender' => 'required',
'password' => 'required',
];
}
public function messages()
{
return [
'name.required' => 'Name is Must',
'name.min' => 'Name Must be 5 Chr.',
];
}
}
Here you will get complete video tutorial on Youtube.
If you face any issues while implementing, please comment your query.
Thank You for Reading
Top comments (0)