Laravel 9 Form Validation Tutorial with Example

In this tutorial you will learn about the Laravel 9 Form Validation Tutorial with Example and its application with practical example.

In this Laravel 9 form validation example tutorial I’ll show you how to validate form data in laravel 9 application. In this laravel form validation tutorial you will learn to validate laravel form with built in laravel validation available in laravel. In this Laravel 9 form validation tutorial I’ll share various validation implementation and use case.

Laravel 9 Form Validation Tutorial

Generally Form validation is performed at server side, but can be performed at both the server and client side. In this laravel form validation tutorial we will be learning about laravel server side validations. In this tutorial, I’ll show you how to define laravel validation rules, how to validate form input and to display validation errors.

Laravel 9 comes many built-in validation rules that can be easily implemented. In this example, we have used some of following validation rules –

required:- It set input field is required.
min:- Validate minimum character length.
max:- Validate maximum character length.
email: It set input must be a valid email address.
unique: It checks for unique value against a database column.
numeric: Input value must numeric.
same:- It validates two input fields value must be same.

Laravel 9 Form Validation Example

In this step by step tutorial I’ll show you how to implement validations in laravel form.

  • Download Laravel 9 Application
  • Setup Database with App
  • Create Model & Migration
  • Create Form Routes
  • Create Form Controller By Artisan Command
  • Create Form Blade File
  • Run Development Server

Install Laravel 9

First of all we need to create a fresh laravel project, download and install Laravel 9 using the below command

Configure Database In .env file

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Create Model & Migration

Now, in this step we will create model and migration file for form. Please run the following command:

Once above command is executed there will be a migration file created inside database/migrations/ directory, just open create_employees_table.php migration file and update the function up() method as following:

Now, run the migration to create database table using following artisan command:

Create Form Routes

After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.

routes/web.php

Create Form Controller By Artisan Command

Next, we have to create a controller to display form and to handle form validation and submit operations. Lets Create a controller named FormController using command given below –

Open the FormValidationController.php file and put the following code in it.

app/Http/Controllers//FormController.php

Create Form Blade File

Now, create form blade view file to display form and submit to database. So, Go to resources/views and create form.blade.php and update the following code into it:

The following below code will display validation error message on blade view file:

Start Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

http://localhost:8000/form

In this tutorial we have learn about the Laravel 9 Form Validation Tutorial with Example and its application with practical example. I hope you will like this tutorial.