Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?.

like image 780
JOE Avatar asked Aug 07 '14 16:08

JOE


People also ask

What is the method used for specifying custom message for validation errors in form request?

After checking if the request failed to pass validation, you may use the withErrors method to flash the error messages to the session. When using this method, the $errors variable will automatically be shared with your views after redirection, allowing you to easily display them back to the user.

How can I validate my name in laravel?

php", to replace the :attribute name (input name) for a proper to read name (example: first_name > First name) . It seems very simple to use, but the validator doesn't show the "nice names". And the validation in the controller: $validation = Validator::make($input, $rules, $messages);

What is the method used to configure validation rules in form request laravel?

Laravel Form Request class comes with two default methods auth() and rules() . You can perform any authorization logic in auth() method whether the current user is allowed to request or not. And in rules() method you can write all your validation rule.


4 Answers

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); 
like image 137
uiroshan Avatar answered Sep 18 '22 17:09

uiroshan


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' ], 
like image 39
hamid Reza Kamali Avatar answered Sep 19 '22 17:09

hamid Reza Kamali


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.

like image 42
GeraldBiggs Avatar answered Sep 20 '22 17:09

GeraldBiggs


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'
            ]);
like image 26
Paul Dela Vega Avatar answered Sep 17 '22 17:09

Paul Dela Vega