Laravel 8.x Server Side Form Validation Example

Hello Artisan

In this tutorial i will discuss about Laravel 8 form validation from step by step. We will look at example of laravel 8 form validation with error message in details. I will use custom error message to show form error message before submit the data into database.

I will display error message with each field. we will use has() for checking the error message in laravel 8. Here, now i am going to show you very simple and easy custom example of form validation so, you can simply use in your laravel 8 project.

We should use form validation before form submit. Data sanitization makes our website more secure. We everytime it is good practice to use clinet side as well as server side form validatio. Now i will discuss Laravel 8 form validation.

laravel-8-form-validation-example

 

Step 1 : Create Routes

Do you know Laravel has changed routing system. I am coming with a new tutorial about routing system in laravel. Have a look on new routing system in laravel. In this example we are learning simple and easy example of validation in laravel 8 so just add following both route in your web.php file.

routes/web.php

use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\HomeController;
  
Route::get('user/create', [ HomeController::class, 'create' ]);
Route::post('user/create', [ HomeController::class, 'store' ]);

 

Step 2 : Create Controller

Now we will add two controller method which we did write in web.php file, one will just display blade file with get request, and another for post request to save data into database, i write validation for that, so simply add both following method on it.

app/Http/Controllers/HomeController.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('test');
    }
    
    /**
     * 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.');
    }
}

 

Read also : Laravel 7.x Client Side Form Validation with jQuery Example

 

Step 3 : Create Blade File

In this final step now here i will create test.blade.php file and here we will create bootstrap simple form with error validation message. So, let's create following file:

resources/views/test.blade.php

 

Read also : Laravel 7.x Form Validation with Custom Error Messages

 

Hope this laravel 8 form validation tutorial can help you.

 

#laravel #laravel-8 #laravel-8x