26

I was trying form validation in laravel. I have a input text field in my form called 'Category' and i'm given the field name as 'cat' as short.

And i defined the validation rules like this.

public static $rules=array(
         "name"=>"required|min:3",
          "cat"=>"required"
           );

When the validation fails i'm getting error message like this

The name field is required.
The cat field is required.

But i want to display it as "The category field is required" instead of 'cat'.
How can i change 'cat' to 'Category' in error message ?.

JOE
  • 489
  • 4
  • 7
  • 16

9 Answers9

48

You can specify custom error message for your field as follows.

$messages = array(
    'cat.required' => 'The category field is required.',
);

$validator = Validator::make($input, $rules, $messages);

Please see Custom Error Messages section in laravel documentation for more information.

Or you can keep a mapping for your field names like below. And you can set those into you validator. So you can see descriptive name instead of real field name.

$attributeNames = array(
   'name' => 'Name',
   'cat' => 'Category',     
);

$validator = Validator::make ( Input::all (), $rules );
$validator->setAttributeNames($attributeNames);
uiroshan
  • 5,021
  • 2
  • 39
  • 37
  • 1
    Ok thats helpful. But i need to specify separate message for required, min, max etc right?. Thats time consuming for large forms. Is there any method you know to change it in single line for all type of errors?. – JOE Aug 07 '14 at 19:06
  • you just want to map cat into category field. is it? So default message will show with the 'categroy' text. – uiroshan Aug 07 '14 at 19:12
  • this is the kind of solution I've been dreaming for :D thanks! – Altin Jun 01 '21 at 08:25
  • 1
    The attributeNames is now the third parameter of the `make()` method. It no longer needs to be set using the setter. – Jason Dec 15 '22 at 13:41
27

you can customize every message ,also change the attribute fields name in validation.php (resources/lang/en/). for set the attribute in validation.php

'attributes' => [
   'name' => 'Name',
   'cat' => 'Category',
   'field_name'=>'your attribute'
],
22

I'm using this to deal with dynamic row addition in forms. This answer is provided in context of managing larger forms. This answer is for Laravel 5

Form request

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Response;

class SomeThingFormRequest extends Request {

public function authorize()
{
 //be nice
}

public function rules()
{
//my loop building an array of rules ex: $rules['thing.attribute.'.$x]
}

public function attributes()
{
    return [
        'thing.attribute'.$x => 'Nice Name',
    ];
}

Hope this help steer you in the right direction if you are using Laravel 5. I'm currently working on testing it out. The documentation seems to be amiss regarding this potential?

I did some digging in the framework (Illuminate\Foundation\Http\FormRequest.php) and (Illuminate\Validation\Validator.php) for the clues.

GeraldBiggs
  • 1,140
  • 1
  • 8
  • 9
19

Here is an Alternative $this->validate(request(), [rules], [custom messages], [Custom attribute name]);

            $this->validate(request(), [
                'fname' => "required|alpha_dash|max:20",
                'lname' => "required|alpha_dash|max:30",
                'extensionName' => "required|alpha_dash|max:20",
                'specialization' => "max:100",
                'subSpecialization' => "max:100"
            ], [], 
            [
                'fname' => 'First Name',
                'lname' => 'Last Name',
                'extensionName' => 'Extension Name',
                'specialization'=> 'Specialization',
                'subSpecialization'=> 'Sub Specialization'
            ]);
Paul Dela Vega
  • 191
  • 1
  • 3
5

Simply got to resources/lang/en/validation.php

There is a blank array named attributes.

add your attribute name here like 'cat'=>'category'

now all validation messages show category instead of cat.

SuperDJ
  • 7,488
  • 11
  • 40
  • 74
Hitender
  • 189
  • 3
  • 6
1

Here's an alternative:

$data = ['name' => e(Input::get('name')), 'cat' => e(Input::get('cat'))];
$rules = ['name' => 'required|min:3', 'cat' => 'required'];
// pass an item from lang array if you need to
$nice_input_names = ['cat' => trans('lang_file.a_name_for_the_category_input')];

$validator = Validator::make($data, $rules, $messages, $nice_input_names);
Calin Blaga
  • 1,375
  • 12
  • 19
1

I think this will do it:

Validator::make($request->all(), [
  "name" => "required|min:3",
  "cat" => "required"
], [], [
  "cat" => "category"
])->validate();
Magmatic
  • 1,754
  • 3
  • 19
  • 34
1

in Laravel 8 this is the Best Way to Customize Validation Attributes and Message;

$request->validate([
            'cat' => "required| min:7 |max:15"
            //validation Area
        ], 
       [
        //customize Message
       ], 
        [
            'cat' => 'Category'
            //Customize Attributes Name
        ]);
Nazakat
  • 11
  • 2
0

the answer by Paul Dela Vega is big help for me, cant comment because no reputation enough.

no information of it anywhere in Laravel Docs (8.x). maybe this is a good suggestion to laravel, to add this into their documents because it helps a lot for the beginner like me.

SADmiral
  • 41
  • 4