How to display custom error message in laravel ?

Laravel provides a set of rules and associated error messages for form input validation. We can use rules to check validity of the user inputs and display messages in case of invalid input. In order to display custom message, we need to create custom request class where we will define rules and  messages.

We are using following artisan command to create custom request class.

php artisan make:request UserPostRequest

After executing above command, a request class will be created inside App/Http/Requests/ directory.

<?php 

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UserPostRequest extends FormRequest
{
    /** 
     * Determine if the user is authorized to make this request. 
     * 
     * @return bool 
     */
    public function authorize()
    {
        return false;
    }

    /** 
     * Get the validation rules that apply to the request. 
     * 
     * @return array 
     */
    public function rules()
    {
        return [

        ];
    }
}

Now, we have to return true in the authorize function. After that, we need to add rules and custom messages.

Also Read: HTTP Exception And Error Handling In Laravel

Let's consider we have following input fields,

  1. Name
  2. Email
  3. Phone

Suppose, email should be unique and all fields must be filled in our scenario. So, we need to add following rules and associated error messages to our request class.

<?php 
namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UserPostRequest extends FormRequest
{
    /** 
     * Determine if the user is authorized to make this request. 
     * 
     * @return bool 
     */
    public function authorize()
    {
        return true;
    }

    /** 
     * Get the validation rules that apply to the request. 
     * 
     * @return array 
     */
    public function rules()
    {
        return [
            'name' => 'required', 
            'email' => 'required|unique:users', 
            'phone' => 'required|min:10|numeric',
            ];
    }

    /** * Get the messages if validation fails * * @return array */
    public function messages()
    { 
        // write custom messages for each rule
        
        return [ 
            'name.required' => 'please enter name', 
            'email.required' => 'please enter email', 
            'email.unique' => 'email must be unique', 
            'phone.required' => 'please enter phone', 
            'phone.min' => 'please enter at least 10 digit', 
            'phone.numeric' => 'please enter numbers only', 
            ]; 
    } 
}
 

Also Read: Dynamic drop down with laravel and jquery

Now, we have to specify the request class in controller function.

public function postUserData(UserPostRequest) 
{ 
    // Code to save form input to database 
}

And one more thing please do not forget to define location of request class in controller,

use App\Http\Requests\UserPostRequest;

In order to display error messages, we need following code in our view.

<form action="{{URL::to('users/create')}}" class="form-horizontal" method="POST" role="form">
    <div class="col-md-12"> {{ csrf_field() }}
        <div class="col-md-5">
            <div class="form-group"><label for="name" class="control-label"> Name</label> <input id="name" type="text"
                                                                                                 name="name"
                                                                                                 class="form-control"
                                                                                                 value="{{old('name')}}">
                @if($errors->has('name')) <span
                        class="help-block"> <strong>{{ $errors->first('name') }}</strong> </span> @endif
            </div>
        </div>
        <div class="col-md-5 col-md-push-1">
            <div class="form-group"><label for="email" class="control-label"> Email</label> <input id="email"
                                                                                                   type="email"
                                                                                                   name="email"
                                                                                                   class="form-control"
                                                                                                   value="{{old('email')}}">
                @if($errors->has('email')) <span
                        class="help-block"> <strong>{{ $errors->first('email') }}</strong> </span> @endif
            </div>
        </div>
    </div>
    <div class="col-md-12">
        <div class="col-md-5">
            <div class="form-group"><label for="phone" class="control-label"> Phone</label> <input id="phone"
                                                                                                   type="text"
                                                                                                   name="phone"
                                                                                                   class="form-control"
                                                                                                   value="{{old('phone')}}">
                @if($errors->has('phone')) <span
                        class="help-block"> <strong>{{ $errors->first('phone') }}</strong> </span> @endif
            </div>
        </div>
        <div class="col-md-5">
            <div class="form-group">
                <button class="btn btn-default" type="submit"> Create</button>
            </div>
        </div>
    </div>
</form>

In this way, we can display custom error message after validation in laravel.


share :

Sagar Gautam

A Computer Engineer from Nepal.


Leave your Feedback