Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Laravel 9 Form Validation Example Tutorial

Laravel 9 Form Validation Example Tutorial

Posted on March 2, 2022December 11, 2022 By XpertPhp

Today, We explain to you and how to create step by step form validation example in laravel 9(Laravel 9 Form Validation Example). 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

See also  laravel eloquent Union query example
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">
            &nbsp;
        </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

Laravel Tags:laravel 9 example, laravel 9 tutorial, laravel form validation

Post navigation

Previous Post: Laravel 9 Fullcalendar Example Tutorial
Next Post: Laravel 9 Fullcalendar Ajax Example Tutorial

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com