10-Apr-2023
.
Admin
Hi guys, Today i will explained to the Laravel Validation Confirmed Example in your laravel project.Laravel Validation Confirmed Example is so easy to use.so you can just follow my step by step and learn Laravel Validation Confirmed Example.
So let's start to the example and follow to the my all step.
Solution
request()->validate([
'name' => 'required | min:4 | max: 20',
'email' => 'required',
'password' => 'required| min:4|confirmed',
'password_confirmation' => 'required| min:4'
]);
Example :
Step 1: Create Route
Last step to create a route in web.php file and use this code.
Route :routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('users/create', [UserController::class, 'index'])->name('users.create');
Route::post('users/store', [UserController::class, 'store'])->name('user.store');
Step 2: Create a UserController Controller
Next you can require to the User Controller so create a User Controller in just following command through.
Controller : app/Http/Controllers/UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use Validator;
class UserController extends Controller
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
return view('users');
}
public function store(Request $request)
{
$inputs = request()->validate([
'name' => 'required | min:4 | max: 20',
'email' => 'required',
'password' => 'required| min:4|confirmed',
'password_confirmation' => 'required| min:4'
]);
dd('done');
}
}
Step 4: Create a User Blade File
Next you can require to the user.blade.php so create a user.blade.php in just following step.
View :resources/views/user.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel Validation Confirmed Example</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>Laravel Validation Confirmed Example</h1>
<div class="card-body">
@if (count($errors) > 0)
<div class="row">
<div class="col-md-12">
<div class="alert alert-danger alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
@foreach($errors->all() as $error)
{{ $error }} <br>
@endforeach
</div>
</div>
</div>
@endif
{!! Form::open(array('route' => 'user.store','method'=>'POST')) !!}
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Name</label>
{{ Form::text('name', null ,['class'=>'form-control', 'placeholder'=>'Name'] ) }}
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>Email</label>
{{ Form::text('email', null ,['class'=>'form-control', 'placeholder'=>'Email'] ) }}
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>Password</label>
{{ Form::text('password', null ,['class'=>'form-control', 'placeholder'=>'Password'] ) }}
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>Password Confirmation</label>
{{ Form::text('password_confirmation', null ,['class'=>'form-control', 'placeholder'=>'Password Confirmation'] ) }}
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 text-center mt-2 mb-3">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
{!! Form::close() !!}
</div>
</div>
</body>
</html>
So, finally we are done with our code we can get below output.
php artisan serve
Browser url run : http://localhost:8000/users/create
#Laravel 8
#Laravel 7