Laravel 8 Form Validation Example

Laravel 6 , Laravel , Laravel 7 , Laravel 8

Nicesnippets

55717

10-09-2020


Laravel 8 Form Validation Example

Hi Dev,

In this tutorial, I would like to share with you how to use form validation in laravel 8. I will let you know how to implement form input validation in laravel 8. In this tutorial, I will show you how to define laravel validation rules, how to validate form input and to display validation errors.

we will help you to give example of form validation with error message in laravel 8. In this blog, laravel 8 form validation example tutorial.

Here I will give you full example for laravel 8 form validation example. So let's follow bellow step by step.

Step 1 : Install Laravel 8


In the first step, we need to get fresh laravel 8 version application So let's open terminal and run bellow command to install fresh laravel project.

composer create-project --prefer-dist laravel/laravel blog

Step 2 : Database Configuration

In second step, we will make database Configuration for example database name, username, password etc for our crud application of laravel 8 So lets open .env file and fill all deatils like as bellow:

.env

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=here your database name(blog)

DB_USERNAME=here database username(root)

DB_PASSWORD=here database password(root)

Step 3: Add Resource Route

Here, we need to add resource route for form validation in user create. so open your "routes/web.php" file and add following route.

routes/web.php

use App\Http\Controllers\HomeController;

Route::get('user/create', [ HomeController::class, 'create' ]);

Route::post('user/store', [ HomeController::class, 'store' ]);

Step 4: Add Controller

In this step, now we should create new controller as HomeController. So run bellow command and create new controller. bellow controller for create resource controller.

php artisan make:controller HomeController

After bellow command you will find new file in this path "app/Http/Controllers/HomeController.php".

In this controller will create seven methods by default as bellow methods:

1)create()

2)store()

So, let's copy bellow code and put on HomeController.php file.

app/Http/Controllers/HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

class HomeController extends Controller

{

/**

* Show the application dashboard.

*

* @return \Illuminate\Http\Response

*/

public function create()

{

return view('createUser');

}

/**

* 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 = User::create($validatedData);

return back()->with('success', 'User created successfully.');

}

}

Step 5: Add Blade Files

In last step. In this step we have to create just blade files. So mainly we have to create createUser file. So finally you have to create createUser bellow blade file:

So let's open createUser.blade.php following file and put bellow code.

resources/views/createUser.blade.php

resources/views/createUser.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel 8 Form Validation Example - NiceSnippets.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 rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha512-MoRNloxbStBcD8z3M/2BmnT+rg4IsMxPkXaGh2zD6LGNNFE80W3onsAhRcMAMrSoyWL9xD7Ert0men7vR8LUZg==" crossorigin="anonymous" />

</head>

<body>

<div class="container">

<div class="row mt-5">

<div class="col-8 offset-2 mt-5">

<div class="card">

<div class="card-header bg-info">

<h3 class="text-white">Laravel 8 Form Validation Example - NiceSnippets.com</h3>

</div>

<div class="card-body">

@if(Session::has('success'))

<div class="alert alert-success">

{{ Session::get('success') }}

@php

Session::forget('success');

@endphp

</div>

@endif

<form method="POST" action="{{ url('user/store') }}">

{{ csrf_field() }}

<div class="form-group">

<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">

<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">

<strong>Email:</strong>

<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 text-center">

<button class="btn btn-success btn-submit">Save</button>

</div>

</form>

</div>

</div>

</div>

</div>

</div>

</body>

</html>

Now we are ready to run our form validation example with laravel 8 so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

localhost:8000/user/create

You will see layout as like bellow:

Output

It will help you...


Recommended Posts