2

I am new to laravel and working on apis, I have made an api in which i have implemented validation.Everything is working fine but i am stuck on a little thing. I want to to change the key name in the validation error. For example For the "unique" validation error. This is what now showing

enter image description here

I want to rename "email"(text) key with "message"(text)

I have tried so many thing in illuminate/support/validation.php

messagebag.php file but if it changes then error show of "data undefined".

The links i followed are

https://stillat.com/blog/2018/04/21/laravel-5-message-bags-adding-messages-to-the-message-bag-with-add

https://laracasts.com/discuss/channels/laravel/custom-validation-message-for-array-using-different-key?page=0

Override laravel validation message

This is the validation code

$validator = Validator::make($request->all(), [ 
      'first_name' => 'required',
      'last_name' => 'required',
      'email' => 'required|email|unique:users',
      'fcm_token' => 'required',
      'password' => 'required',  
      'c_password' => 'required|same:password' 
    ]); 
10
  • 1
    You should add atleast one of your tried code so, people could help you on that code, your question seems to asking people guess about your codes and setup, and write you full codes. Commented Jan 25, 2020 at 13:00
  • Why exactly do you "need" this? It's not going to work if you have more than 1 field being validated. Commented Jan 25, 2020 at 13:09
  • @Qirel i want to rename the key in the response. I dont want to validate two fields. Commented Jan 25, 2020 at 13:44
  • @Dilek please let me know if you are not clear about my question. I am not asking for any script. I am asking about the way to do it. I tried the same code in same files that are mentioned and also given in the provided links. I want to rename "email"(text) key with "message"(text) Commented Jan 25, 2020 at 13:45
  • 1
    I don't understand the purpose though, the index is the name of the invalid field -- why would you need to change it? My point is that, should there at one point become more than 1 field, you won't be able to separate which message belongs to which field. Commented Jan 25, 2020 at 14:10

6 Answers 6

4

You can manually loop over the error MessageBag and construct the response to replace a key

    $validator = Validator::make($request->all(), [ 
      'first_name' => 'required',
      'last_name'  => 'required',
      'email'      => 'required|email|unique:users',
      'fcm_token'  => 'required',
      'password'   => 'required',  
      'c_password' => 'required|same:password' 
    ]);   

    $errors = [];
    foreach ($validator->errors()->messages() as $key => $value) {
        if($key == 'email')
            $key = 'message';
        $errors[$key] = is_array($value) ? implode(',', $value) : $value;
        //implode is for when you have multiple errors for a same key
        //like email should valid as well as unique
    }

    $result = array("status" => count($errors)?0:1, 'data'=>$errors);
    return $result;
Sign up to request clarification or add additional context in comments.

2 Comments

when email field is empty then the program does not reach to the method. It directly shows from validation exception.I already tried this.
oh that means the validation is running somewhere else but it's hard to guess where with the information available
1

In resources - lang - en (Or Any Langauge) - validation.php

Put This code in bottom:

'attributes' => [
    'email' => 'The Email', 
],

Comments

1

In order to change the key of the error message of a certain field, you can transfer the validation of this particular field from the request to the controller (or service). This way you can throw an error with any name you want.

For example:

if (#your validation of the field failed#) {
  throw ValidationException::withMessages(['your_key' => 'your message']);
}

And don't forget this line:

use Illuminate\Validation\ValidationException;

The error now will look like this:

{
    "message": "The given data was invalid.",
    "errors": {
        "your_key": [
            "your message"
        ]
    }
}

Comments

0

In addition to @wael-serag answer: https://stackoverflow.com/a/59948967/8915498

Be aware that in the latest versions of laravel, the lang directory does not include by default

Laravel documentation on customizing validation errors

So don't forget to run

php artisan lang:publish

Comments

-1

if I understand the problem well. This will help you:

a https://laravel.com/docs/7.x/validation#rule-unique

In your case:

'email' => 'required|email|unique:users,message'

unique:table,column,except,idColumn

Hope it helps.

Comments

-2

In Laravel, you can try and validate your email this way:

'email'=>'required|email'

4 Comments

"I want to replace "email" key with "message"" How does this change email to message?
@kerbholz edited question I want to rename "email"(text) key with "message"(text)
@dev: I know, that's what I was saying. Netwons code does nothing related to this
@kerbholz yes, do you have any idea?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.