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

I tried form validation in laravel. I have a text input field in my form called "Category" and I am assigned the field name as "cat" as short.

And I defined validation rules like this.

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

When the check fails, I get an error message similar to this

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

But I want to display it as "Category field required" instead of "cat".
How can I change "cat" to "Category" in the error message ?.

+6
source share
5 answers

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

 $messages = array( 'cat.required' => 'The category field is required.', ); $validator = Validator::make($input, $rules, $messages); 

See the custom error message section of the laravel documentation for more information .

Or you can save the mapping for field names as shown below. And you can install them in the validator. Thus, you can see a descriptive name instead of the name of a real field.

 $attributeNames = array( 'name' => 'Name', 'cat' => 'Category', ); $validator = Validator::make ( Input::all (), $rules ); $validator->setAttributeNames($attributeNames); 
+16
source

I use this to deal with adding dynamic lines to forms. This answer is provided in the context of large form management. 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 helps you in the right direction if you are using Laravel 5. I am currently working on testing it. Perhaps the documentation does not match this potential?

I made some breakthroughs under (Illuminate \ Foundation \ Http \ FormRequest.php) and (Illuminate \ Validation \ Validator.php) for tips.

+5
source

you can customize each message, also change the attribute field name in the validation.php file (resources / lang / en /). to set an attribute in validation.php

 'attributes' => [ 'name' => 'Name', 'cat' => 'Category', 'field_name'=>'your attribute' ], 
+2
source

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

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

There is an empty array called attributes .

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

now all validation messages show a category instead of cat.

0
source

Source: https://habr.com/ru/post/973507/


All Articles