Hey everyone! Have you ever wondered how to ensure that the data users submit through your web forms is valid and secure? In this guide, I'll walk you through the process step by step, showing you how to validate forms effectively to maintain data integrity and security in your Laravel applications.
Throughout this guide, we'll cover everything you need to know about form validation in Laravel 10.
So, let's see how to validate a form in laravel 10, laravel 10 form validation, custom form validation in laravel 8/9/10, laravel validation, and laravel custom validation rule example.
First things first, ensure you have Laravel 10 installed on your system. If not, you can quickly set it up using Composer:
composer create-project --prefer-dist laravel/laravel:^10.0 form-validation-example
Navigate to your project directory:
cd form-validation-example
Next, define a route that corresponds to our form action. Open up routes/web.php
and define a route for handling form submission:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FormValidationController;
/*
|--------------------------------------------------------------------------
| 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('/', function () {
return view('welcome');
});
Route::get('index', [ FormValidationController::class, 'index' ]);
Route::post('store', [ FormValidationController::class, 'store' ])->name('store');
Now, let's create a controller to handle form validation and submission. Run the following Artisan command to generate a controller:
php artisan make:controller FormValidationController
Then, open up app/Http/Controllers/FormValidationController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class FormValidationController extends Controller
{
public function index(){
return view('index');
}
public function store(Request $request){
$validatedData = $request->validate([
'name' => 'required',
'password' => 'required|min:5',
'confirm_password' => 'required|same:password|min:5',
'email' => 'required|email|unique:users'
], [
'name.required' => 'Name field is required.',
'password.required' => 'Password field is required.',
'confirm_password.required' => 'Confirm password field is required.',
'email.required' => 'Email field is required.',
'email.email' => 'Email field must be email address.'
]);
$validatedData['password'] = bcrypt($validatedData['password']);
$user = User::create($validatedData);
return back()->with('success', 'User created successfully.');
}
}
Now, let's create a form to validate. You can create a Blade view file under the resources/views
directory.
resources/views/index.blade.php
<!DOCTYPE html>
<html>
<head>
<title>How to Validate Forms in Laravel 10 - Vidvatek</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container col-md-6">
<h3 class="mt-5">How to Validate Forms in Laravel 10 - Vidvatek</h3>
@if(Session::has('success'))
<div class="alert alert-success">
{{ Session::get('success') }}
@php
Session::forget('success');
@endphp
</div>
@endif
<!-- Way 1: Display All Error Messages -->
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form method="POST" action="{{ route('store') }}">
{{ csrf_field() }}
<div class="mb-3">
<label class="form-label" for="inputName">Name:</label>
<input type="text" name="name" class="form-control @error('name') is-invalid @enderror" placeholder="Name">
<!-- Way 2: Display Error Message -->
@error('name')
<span class="text-danger">{{ $message }}</span>
@enderror
</div>
<div class="mb-3">
<label class="form-label" for="inputEmail">Email:</label>
<input type="text" name="email" class="form-control @error('email') is-invalid @enderror" placeholder="Email">
@error('email')
<span class="text-danger">{{ $message }}</span>
@endif
</div>
<div class="mb-3">
<label class="form-label" for="inputPassword">Password:</label>
<input type="password" name="password" class="form-control @error('password') is-invalid @enderror" placeholder="Password">
<!-- Way 3: Display Error Message -->
@if ($errors->has('password'))
<span class="text-danger">{{ $errors->first('password') }}</span>
@endif
</div>
<div class="mb-3">
<label class="form-label" for="inputPassword">Confirm Password:</label>
<input type="password" name="confirm_password" class="form-control @error('confirm_password') is-invalid @enderror" placeholder="Confirm Password">
<!-- Way 3: Display Error Message -->
@if ($errors->has('confirm_password'))
<span class="text-danger">{{ $errors->first('confirm_password') }}</span>
@endif
</div>
<div class="mb-3">
<button class="btn btn-success btn-submit">Submit</button>
</div>
</form>
</div>
</body>
</html>
You might also like:
- Read Also: How To Validate Form in React JS
- Read Also: How to Upload Multiple Images in Laravel 10
- Read Also: How to Validate Form using Validate.js in Laravel 10
- Read Also: Laravel 10 Password and Confirm Password Validation