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

0 votes

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 ?

Sep 30, 2020 in Laravel by kartik
• 37,510 points
4,383 views

1 answer to this question.

0 votes

Hello @kartik,

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

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

Hope it helps!!

Thank You!!

answered Sep 30, 2020 by Niroj
• 82,880 points

Related Questions In Laravel

0 votes
2 answers

How to solve expected response code 220 but got code “”, with message “” in Laravel?

This problem can generally occur when you ...READ MORE

answered Dec 16, 2020 in Laravel by Gitika
• 65,910 points
40,705 views
0 votes
1 answer

How to get the id when you're validating in the model Validation unique on update using laravel?

Hello @kartik, in Laravel's inbuilt auth system, the ...READ MORE

answered Sep 11, 2020 in Laravel by Niroj
• 82,880 points
6,262 views
0 votes
1 answer

How to return database table name in Laravel?

Hello @kartik, There is a public getTable() method ...READ MORE

answered Sep 24, 2020 in Laravel by Niroj
• 82,880 points
2,646 views
0 votes
1 answer

How to validate an input field if value is not null in Laravel?

Hello @kartik, try using nullable as a rule 'password' ...READ MORE

answered Sep 25, 2020 in Laravel by Niroj
• 82,880 points
15,219 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
21,750 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,647 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,504 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
41,371 views
0 votes
1 answer

How to create custom validation rules with Laravel?

Hii @kartik, Follow are the steps to create ...READ MORE

answered Mar 24, 2020 in Laravel by Niroj
• 82,880 points
921 views
0 votes
1 answer

Error:login in Laravel. How to solve?

Hey, First check if your model login has a field password in ...READ MORE

answered Mar 26, 2020 in Laravel by Niroj
• 82,880 points
7,137 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP