Laravel 6 validation with error message

  • 22-03-2022
  • 959
  • Laravel 6
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

In this tutorial, I will teach you how it works laravel 6 validation with the error message. Laravel 6 provides default validation functions like required, exists, unique, URL, nullable, etc. I will give you some tips on laravel validation and what is the procedure to set a custom message. I will give some steps with an example.

Step 1 : Clone fresh laravel application

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

After clone laravel fresh aplication composer install and generate application key using this command.

php artisan key:generate

Step 2 : Now we move on project directory

routes/web.php

Route::get('form',['as'=>'form','uses'=>'UserController@form']);
Route::post('form/save',['as'=>'form.save', 'uses'=>'UserController@saveForm']);

Step 3 : Create controller

php artisan make:controller UserController 

app/Http/Controllers/UserController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\FrontController;
use App\User;

class UserController extends Controller
{
    public function form(){       
        return view('form');
    }
    public function saveForm(Request $request){
        $input = $request->all();   
        $validator = $this->validate($request,[
            'fullname' => 'required',
            'email' => 'required|unique|email',
        ],[
            // Here you can set a custom message.
            'fullname.required' => 'This fullname must be required.',
            'email.required' => 'This email must be required.',
            'email.email' => 'Please enter valid email',
        ]);
        User::create($input);
        return redirect()->route('form');
    }
}

Now here we are required to submit form so create that.

How To Clone Laravel

In this step we will create form file

Step 4 : Create form

resources/views/form.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Validation - Tutorial</title>
</head>
<body>
    @if($errors->has())
        <ul>>
           @foreach ($errors->all() as $error)
              <li>{{ $error }}</li>
          @endforeach
        </ul>
    @endif
    <form method="post" action="{{ route('form.save') }}">
        {{ csrf_field() }}
        <label>Fullname : </label>
        <input type="text" name="fullname">
        <br>
        <label>Email : </label>
        <input type="email" name="email">

        <button type="submit">Submit</button>
    </form>
</body>
</html>

You can use also individual laravel validation like this.

@if($errors->has('fullname')) {{ $errors->first('fullname') }} @endif
@if($errors->has('email')) {{ $errors->first('email') }} @endif

Here you can also check this laravel more Validation

Hope, it can help you for better laravel validation learning.


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :