In this Laravel 9 validation tutorial, I'll demonstrate how to validate form input and provide an error message before saving it to the database. Therefore, Laravel 9 will sanities our form data before saving it to the database. This example is for you if you don't know how to validate form input in Laravel 9.

You will therefore learn how to implement the Laravel 9 form validation lesson from this Laravel 9 validation tutorial. Laravel 9 form validation will appear, along with an error notice. To create custom error messages for Laravel 9 form validation, simply follow the steps below.

You are aware that Laravel 9 offers a request object so you can use it to provide form validation. I'll use this request validate() to add custom messages and validation rules. Check out the example for adding form validation below.

Download?Laravel 9

To implement this form validation in Laravel 9, you must first download a Laravel 9 application. So use the command below to download it.

composer create-project laravel/laravel example-app

Create Route

Currently, add the routes to control GET and POST requests for call view, as well as add form validation, to the routes/web.php file.

routes/web.php

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

Route::controller(UserController::class)->group(function () {
    Route::get('/', 'create')->name('user.create');
    Route::post('/', 'store')->name('user.store');
});

Create Controller

In order to provide form validation, we will develop a new UserController in this step. We will include two methods called create() and store in this controller (). So let's run the following command to build a new controller.

app/Http/Controllers/UserController.php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Blade;

class UserController extends Controller
{
    public function create()
    {
        return Blade::render('welcome');
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'name' => 'required',
            'password' => 'required|min:5',
            'email' => 'required|email|unique:users'
        ], [
            'name.required' => 'Name field is required.',
            'password.required' => 'Password field is required.',
            'email.required' => 'Email field is required.',
            'email.email' => 'Email field must be email address.'
        ]);

        $data['password'] = bcrypt($data['password']);

        User::create($data);
        
        return back()->with('success', 'User created successfully.');
    }

}

Create View

We now need to develop a view form in this last stage in order to validate our form input. I'll build a straightforward bootstrap form with an error message. Let's make the following file, then:

resources/views/welcome.blade.php

@extends('master')
 
@section('content')
<div class="container">
    
    <h1>Laravel 9 Form Validation Example - codesolutionstuff.com</h1>
   
    @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">
            <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 method="POST" action="{{ route('user.store') }}">
  
        @csrf 
  
        <div class="mb-3">
            <label class="form-label" for="inputName">Name:</label>
            <input 
                type="text" 
                name="name" 
                id="inputName"
                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="inputPassword">Password:</label>
            <input 
                type="password" 
                name="password" 
                id="inputPassword"
                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="inputEmail">Email:</label>
            <input 
                type="text" 
                name="email" 
                id="inputEmail"
                class="form-control @error('email') is-invalid @enderror" 
                placeholder="Email">

            @error('email')
                <span class="text-danger">{{ $message }}</span>
            @endif
        </div>
 
        <div class="mb-3">
            <button class="btn btn-success btn-submit">Submit</button>
        </div>
    </form>
</div>
@endsection

I hope this lesson on Laravel 9 form validation with error message was helpful.


Tags

Recommended Posts

View All

Laravel 9 File Manager Tutorial Example


Learn how to implement a robust file manager in Laravel 9 with our step-by-step tutorial. This example will help you manage and organize files efficie...

Laravel 9 Generate Sitemap XML File Tutorial


Learn how to generate a sitemap.xml file in Laravel 9 with this step-by-step tutorial. Improve your website's SEO and enhance user experience.

Laravel 9 Socialite Login with Facebook Tutorial


In this tutorial, we'll learn how to use the Socialite package in Laravel to login with a Facebook social networking account.

How to perform CURL requests using ixudra/curl in Laravel


Laravel&#39;s ixudra package offers an effective method for making HTTP requests using curl.

Laravel One to Many Polymorphic Relationship


laravel polymorphic relations example, laravel polymorphic one to many, laravel eloquent polymorphic relations, laravel one to many polymorphic