2

Currently I have working validation in my laravel project

using this code I can validate the data.

    $validatorSave1 = Validator::make(
       insert_data1,
           [
               '*.emp_id' => "required|exists:users,company_id",
           ]
     );

     if($validatorSave1->fails()){
                return redirect()
                ->back()
                ->with(['errors'=>$validatorSave1->errors()->all()])
                ->with('modal',$modal);
     }

and this gives me a return like this

enter image description here

How can I edit the error message like this

The selected 1.emp_id is invalid. (Employee Sheet Error)

3 Answers 3

2

As a third argument you can send your custom message like this

$validatorSave1 = Validator::make(
       insert_data1,
           [
               '*.emp_id' => "required|exists:users,company_id",
           ],
           [
               'field.required' => 'You need to fill Name',
               'field.exists' => 'Name already exist in Database'
           ]
     );
Sign up to request clarification or add additional context in comments.

Comments

2

You can set your own custom error message.Below is an example of it :

$validatorSave1 = Validator::make(
       insert_data1,
           [
               '*.emp_id' => "required|exists:users,company_id",
           ]
 [
               '*.emp_id.exists:users' => "Employee Sheet Error",
           ]
     );

Comments

1

Extend your validation maker with custom error messages. Laravel docs

$validatorSave1 = Validator::make(
    insert_data1,
    [
        '*.emp_id' => "required|exists:users,company_id",
    ],
    [
        '*.emp_id.exists' => 'The selected :attribute is invalid. (Employee Sheet Error)',
    ]
);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.