
How to do form validation; Validation is the most important part of a website which validate the data which coming from input fields. Laravel is providing us a best validation with most important rules. It provide us all type of validation of user inputs. We use simple validate method in controller to validate data incoming from user input.
For learning of validation features of laravel we need to look a complete example step by step. In this tutorial we will see how to validate data and insert only unique data into database. Also see how to show error message in view files. Also success message. The old method we gone be use for show old value if any accure.
How to do validation in laravel 9
There are some steps to do form validation in laravel 9 which are given below:
Step 01: Laravel Fresh Proejct
Step 02: View files
index.blade.php
student_list.blade.php
Step 03: Migration Files Creating
Setp 04: Model and Controller
Step 05: Routes Creating
Step 06: Project Testing
Step 01: Laravel Fresh Proejct
First step is we need a laravel 9 fresh project to make form validation. So run the given command and make new project.
Composer create-project laravel/laravel FormValidation
After project creating open it in your favourite code editor.
Step 02: View files
So the next step is to make view files for student form and to show of student list. So visit in your project resource/view and create files and update with the given code.
Index.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css">
<title>Student Form - How to do form validation in laravel</title>
</head>
<body>
<div class="container">
<div class="row pt-3 mt-3">
<div class="col-md-12">
<div class="card">
<div class="card-header text-center bg-info">How to do form validation in laravel 9</div>
<div class="card-body">
<form action="{{url('/store')}}" class="form" method="POST" enctype="multipart/form-data">
@csrf
<div class="form-group mb-3">
<label for="name">Student Name</label>
<input type="text" value="{{ old('name') }}" name="name" id="" class="form-control">
@if ($errors->has('name'))
<p class="text-danger">{{$errors->first('name')}}</p>
@endif
</div>
<div class="form-group mb-3">
<label for="name">Student Email</label>
<input type="email" name="email" id="" value="{{ old('email') }}" class="form-control">
@if ($errors->has('email'))
<p class="text-danger">{{$errors->first('email')}}</p>
@endif
</div>
<div class="form-group mb-3">
<label for="name">Student Phone</label>
<input type="number" name="phone" id="" value="{{ old('phone') }}" class="form-control">
@if ($errors->has('phone'))
<p class="text-danger">{{$errors->first('phone')}}</p>
@endif
</div>
<div class="form-group mb-3">
<label for="name">Student Image</label>
<input type="file" name="image" id="" class="form-control">
@if ($errors->has('image'))
<p class="text-danger">{{$errors->first('image')}}</p>
@endif
</div>
<div class="form-group">
<input type="submit" value="Save Student" class="btn btn-info form-control">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
Student_list.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css">
<title>Student List - How to do form validation in laravel</title>
</head>
<body>
<div class="container">
<div class="row pt-3 mt-3">
<div class="col-md-12">
<div class="card">
<div class="card-header text-center bg-info">How to do form validation in laravel 9</div>
<div class="card-body">
<div class="m-3">
@if (session('message'))
<div class="alert alert-info">
<h5 class="text-center">{{ session('message') }}</h5>
</div>
@endif
</div>
<table class="table">
<thead>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Image</th>
</thead>
@foreach ($student as $student_list)
<tr>
<td>{{ $student_list->id }}</td>
<td>{{ $student_list->name }}</td>
<td>{{ $student_list->email }}</td>
<td>{{ $student_list->phone }}</td>
<td>
<img src="{{ asset('storage/images/' . $student_list->image) }}" width="200px"
height="200px" alt="">
</td>
</tr>
@endforeach
</table>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
Step 03: Migration Files Creating
Next step is to make migration file which we use to make a table in database so after file creating we will define which columns are required in our table so run the given command:
php artisan make:migration create_studens_table
So visit Database/migrations and open your created file and update with given code:
public function up()
{
Schema::create('studens', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email');
$table->string('phone');
$table->string('image');
$table->timestamps();
});
}
Setp 04: Model and Controller
Next step is to make model and controller for our project run the given command.
Php artisan make:model Studen -c
After controller creating visit App/Http/Controller/ and open StudenController and update with given code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Studen;
class StudenController extends Controller
{
public function index(){
$student = Studen::all();
return view('student_list')->with('student',$student);
}
public function form(){
return view('index');
}
public function store(Request $request){
$request->validate([
'name' => 'required',
'email' => 'required|email|unique:studens',
'phone' => 'required|unique:studens',
'image' => 'required|image|mimes:png,jpg,jpeg,svg,gif|max:2048'
]);
$student = new Studen;
$student->name = $request->name;
$student->email = $request->email;
$student->phone = $request->phone;
$image = $request->image;
$image_name = $image->getClientOriginalName();
$image->storeAs('public/images', $image_name);
$student->image = $image_name;
$student->save();
return redirect('/')->with('message','New Student Record is Saved!');
}
}
Step 05: Routes Creating
Then visit Routes directory in your project and open web.php and then update with given code:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\StudenController;
Route::get('/', [StudenController::class, 'index']);
Route::get('/form', [StudenController::class, 'form']);
Route::post('/store', [StudenController::class, 'store']);
Step 06: Project Testing
So its time to test our project is it working properly or not. So run the given command:
php artisan serve
Open the link: http://localhost:8000/
Thank you for reading this tutorial
You may also like this:
Login and Registration system in php
How to show models data into blade file in laravel
How to Install Bootstrap 5 in Laravel 9 With Vite
Laravel 9 Custom Login and Registration System
No Comments Yet.