How to give custom field name in laravel form validation error message

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.


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);

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'
],