Laravel 9 Form Validation With Error Messages

Hello dev, Today we are going to learn Laravel 9 Form Validation With Error Messages. This tutorial will cover on how to validated form in laravel 9 with error messages.

This article goes in detailed on laravel 9 form validation with errors messages. We will look at example of laravel 9 form validation with error message text. if you have question about form validation laravel 9 then i will give simple example with solution.

You can also define laravel 9 form validation custom errors messages. we will display errors message with each field. we will use laravel 9 form request custom validation.

Here, i am going to show you very simple example of form validation so, you can simply use in your laravel 9 project.

Steps on Laravel 9 Form Validation With Error Messages:

  • Step 1: Installing fresh new Laravel 9 Application
  • Step 2: Create Routes
  • Step 3: Create Controller
  • Step 4: Create Blade File
  • Step 5: Testing
  • Step 6: Conclusion

Also Read: Laravel 9 User Roles and Permissions Tutorial Example

Step 1: Installing fresh new Laravel 9 Application

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app

Step 2: Create Routes

routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\StudentController;

/*
|--------------------------------------------------------------------------
| 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('student/create',[StudentController::class,'create']);
Route::post('student/store',[StudentController::class,'store'])->name('student.store');

Also Read: How to Use Inner Join In Laravel 9

Step 3: Create Controller

Now we will add two controller method, one will just display blade file with get request, and another for post request, I write validation for that, so simply add both following method on it.

app/Http/Controllers/StudentController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Student;
  
class StudentController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('createStudent');
    }
    
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validatedData = $request->validate([
                'name' => 'required',
                'password' => 'required|min:5',
                'email' => 'required|email|unique:users'
            ], [
                'name.required' => 'Name is required',
                'password.required' => 'Password is required'
            ]);
  
        $validatedData['password'] = bcrypt($validatedData['password']);
        $user = Student::create($validatedData);
      
        return back()->with('success', 'Student created successfully.');
    }
}

Also Read: Laravel 9 Vue JS Form Validation Example

Step 4: Create Blade File

Now here we will create createUser.blade.php file and here we will create bootstrap simple form with error validation message. So, let’s create following file:

resources/views/createStudent.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 9 Form Validation With Error Messages - LaravelTuts.com</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/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container">
        <div class="row justify-content-center mt-5">
            <div class="col-md-8 mt-5">
                <h3>Laravel 9 Form Validation With Error Messages - LaravelTuts.com</h3>
   
                @if(Session::has('success'))
                    <div class="alert alert-success">
                        {{ Session::get('success') }}
                        @php
                            Session::forget('success');
                        @endphp
                    </div>
                @endif
           
                <form method="post" action="{{ route('student.store') }}" class="mt-4">
          
                    {{ csrf_field() }}
          
                    <div class="form-group mb-3">
                        <label>Name:</label>
                        <input type="text" name="name" class="form-control" placeholder="Name">
                        @if ($errors->has('name'))
                            <span class="text-danger">{{ $errors->first('name') }}</span>
                        @endif
                    </div>
            
                    <div class="form-group mb-3">
                        <label>Email:</label>
                        <input type="text" name="email" class="form-control" placeholder="Email">
                        @if ($errors->has('email'))
                            <span class="text-danger">{{ $errors->first('email') }}</span>
                        @endif
                    </div>
                    <div class="form-group mb-3">
                        <label>Password:</label>
                        <input type="password" name="password" class="form-control" placeholder="Password">
                        @if ($errors->has('password'))
                            <span class="text-danger">{{ $errors->first('password') }}</span>
                        @endif
                    </div>
           
                    <div class="form-group">
                        <button class="btn btn-success btn-submit btn-sm">Submit</button>
                    </div>
                </form>
            </div>    
        </div>
    </div>
</body>
</html>

Also Read: Laravel 9 Get env Variable in Blade File Example

Step 5: Testing

Now everything is done! we are going to test our laravel 9 application. First run the laravel serve with the following command to start the server.

php artisan serve

Now, open the following URL in any web browser to test the application.

http://127.0.0.1:8000/student/create

Preview:

Laravel 9 Form Validation With Error Messages
Laravel 9 Form Validation With Error Messages

Step 6: Conclusion

Today, We had learn Laravel 9 Form Validation With Error Messages. Hope this tutorial helped you with learning Laravel 9. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our YouTube Channel and follow us on social network Facebook and Instagram.

Also Read: Laravel 9 Remove Public from URL using htaccess

3 Comments

Leave a Reply