Laravel 9 Form Validation Example Tutorial
Today, We explain to you and how to create step by step form validation example in laravel 9(Laravel 9 Form Validation Example Tutorial). When a visitor fills up a form, at that time we need to Server Side Form Validation because For which the visitor cannot enter any blank or incorrect data. so in this example, we are using the Laravel 9 Server Side Form Validation Example.
Here we are using all and have methods for form validation. if you want to customize an error message, then you can use the below example through you can make it.
So you can follow the below step.
Overview
Step 1: Create Routes
Step 2: Create a Model and Controller
Step 3: Create Blade Files
Step 4: Run Our Laravel Application
Step 1: Create Routes
Add the following route code in the “routes/web.php” file.
1 2 3 4 | use App\Http\Controllers\StudentController; Route::get('student',[StudentController::class, 'create']); Route::post('student',[StudentController::class, 'store']); |
Step 2: Create a Model and Controller
Here below command help to create the controller and model.
1 | php artisan make:controller StudentController --resource --model=Student |
Student.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php namespace App/Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Student extends Model { use HasFactory; protected $fillable = [ 'first_name','last_name', 'address' ]; } ?> |
StudentController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | <?php namespace App\Http\Controllers; use App\Models\Student; use Illuminate\Http\Request; class StudentController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { // $students = Student::all(); return view('student.list', compact('students','students')); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { // return view('student.create'); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // $request->validate([ 'txtFirstName'=>'required', 'txtLastName'=> 'required', 'txtAddress' => 'required' ], [ 'txtFirstName.required' => 'FirstName is required', 'txtLastName.required' => 'LastName is required', 'txtAddress.required' => 'Address is required' ]); $student = new Student([ 'first_name' => $request->get('txtFirstName'), 'last_name'=> $request->get('txtLastName'), 'address'=> $request->get('txtAddress') ]); $student->save(); return redirect('/student')->with('success', 'Student has been added'); } } ?> |
Step 3: Create Blade Files
So finally, first we will create the new directory “resources/views/student/layouts” and that directory in creating a “resources/views/student/layouts/app.blade.php” file. and the second time we will create a list.blade.php in the “resources/ views/student/” directory.
layout.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <!DOCTYPE html> <html lang="en"> <head> <title>laravel 9 form validation Example Tutorial - XpertPhp</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script> </head> <body> <div class="container"> @yield('content') </div> </body> </html> |
create.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | @extends('student.layouts.app') @section('content') <div class="row"> <div class="col-lg-11"> <h2>Add New Student</h2> </div> <div class="col-lg-1"> </div> </div> @if ($errors->any()) <div class="alert alert-danger"> <strong>Whoops!</strong> There were some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form action="{{ route('student.store') }}" method="POST"> @csrf <div class="form-group"> <label for="txtFirstName">First Name:</label> <input type="text" class="form-control" id="txtFirstName" placeholder="Enter First Name" name="txtFirstName"> @if ($errors->has('txtFirstName')) <span class="error">{{ $errors->first('txtFirstName') }}</span> @endif </div> <div class="form-group"> <label for="txtLastName">Last Name:</label> <input type="text" class="form-control" id="txtLastName" placeholder="Enter Last Name" name="txtLastName"> @if ($errors->has('txtLastName')) <span class="error">{{ $errors->first('txtLastName') }}</span> @endif </div> <div class="form-group"> <label for="txtAddress">Address:</label> <textarea class="form-control" id="txtAddress" name="txtAddress" rows="10" placeholder="Enter Address"></textarea> @if ($errors->has('txtAddress')) <span class="error">{{ $errors->first('txtAddress') }}</span> @endif </div> <button type="submit" class="btn btn-default">Submit</button> </form> @endsection |
Step 4: Run Our Laravel Application
We can start the server and run this example using the below command.
1 | php artisan serve |
Now we will run our example using the below Url in the browser.
1 | http://127.0.0.1:8000/student |