Top Menu

In this tutorial, I would like to interduce with you how to use 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 input and to display validation errors.

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

So let’s follow bellow step by step.

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
{

   
    public function create()
    {
        return view('createUser');
    }


   
    public function store(Request $request)
    {

        $validatedData = $request->validate([

                'name' => 'required',

                'password' => 'required|min:5',

                'email' => 'required|email|unique:users'

            ], [ //custome error massage

                '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.');

    }

}

List of Validation Rules

Eamil Validation Example:


 'email' => 'required|email|unique:users'

Laravel Mobile/Phone Number Validation Example

        $request->validate([
                'phone' => 'required|digits:10',
             
            ]);

About The Author

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Close