Tilak Raj
Tilak Raj

Reputation: 1499

Add a custom validation error message laravel

I can do something like this to validate something on the controller.

$this->validate($request,[
'myinput'=>'regex:some pattern'
]);

and the output of this would be something like this

The myinput format is invalid.

What i want was to show something of my own message

Only some pattern allowed

How do i achieve this on laravel?

Upvotes: 2

Views: 12877

Answers (2)

nokieng
nokieng

Reputation: 2136

There are many techniques to customize validator messages.

Validate inside the controller

It would be looked like this

$validate = Validator::make($request->all(), [
    'name'=>'required|max:120',
    'email'=>'required|email|unique:users,email,'.$id,
    'password'=>'nullable|min:6|confirmed'
], 
[
    'name.required' => 'User name must not be empty!',
    'name.max' => 'The maximun length of The User name must not exceed :max',
    'name.regex' => 'Use name can not contain space',
    'email.required' => 'Email must not be empty!',
    'email.email' => 'Incorrect email address!',
    'email.unique' => 'The email has already been used',
    'password.min' => 'Password must contain at least 6 characters',
    'password.confirmed' => 'Failed to confirm password'
]);

  • The first param is inputs to validate
  • The second array is validator rules
  • The last params is the customized validator messages
  • In which, the synctax is [input_variable_name].[validator_name] => "Customized message"

    Second apprach: using InfyOm Laravel Generator

    I like this approach the most. It provides useful tools for generating such as Controller, Models, Views, API etc. And yet, create and update Request file. in which the Request file is using Illuminate\Foundation\Http\FormRequest where this class is extended from Illuminate\Http\Request.

    It is mean that we can access Request in this file and perform validating for the incoming requests.
    Here is the most interesting part to me.
    The generated request file contains rules function, for example like this

    public function rules() {
        return [
            'name' => 'required|unique:flights,name|max:20',
            'airline_id' => 'nullable|numeric|digits_between:1,10',
        ];
    }
    

    This function actually returns the validator-rules and validate them against the inputs. And you can override function messages from Illuminate\Foundation\Http\FormRequest to customize the error message as you need:

    public function messages()
    {
        return [
            'required' => "This field is required",
            \\... etc
        ];
    }
    



    Nonetheless, you can do many nore with the generated request files, just refer to file in vendor folder vendor/laravel/framework/src/Illuminate/Foundation/Http from your project.

    Here is the Infyom github link InfyOmLabs - laravel-generator

    Upvotes: 9

    Alexey Mezenin
    Alexey Mezenin

    Reputation: 163978

    You can add custom validation messages to language files, like resources/lang/en/validation.php.

    Another way to do that, from docs:

    'custom' => [
        'email' => [
            'regex' => 'Please use your company email address to register. Webmail services are not permitted.'
        ],
        'lawyer_legal_fields' => [
            'number_of_areas' => 'You\'re not allowed to select so many practice areas'
        ],
    ],
    

    You may customize the error messages used by the form request by overriding the messages method.

    public function messages()
    {
        return [
            'title.required' => 'A title is required',
            'body.required'  => 'A message is required',
        ];
    }
    

    https://laravel.com/docs/5.3/validation#customizing-the-error-messages

    Upvotes: 5

    Related Questions