Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize error messages in Laravel?

Tags:

php

laravel

Here is my code:

public function store(Request $request){

    $this->validate($request, [
        'name' => 'required',
        'email' => 'required|email',
        'content' => 'required|min:10',
    ]);

    $new_array = array();
    $new_array['name'] = $request->name;
    $new_array['email'] = $request->email;
    $new_array['content'] = $request->content;
    contact::create($new_array);

    return back();
}

Now I need to set some new error-messages for those filters (ex: required, email, ..).

Currently, this is the default error message for required:

The name field is required.

How can I change it?

like image 224
stack Avatar asked Oct 19 '16 07:10

stack


1 Answers

Crate rules

public function user_registration_rules(array $data)
{
  $messages = [
    'full-name.required' => 'Please enter full name',     
    'address.required' => 'Please enter address'
  ];

  $validator = Validator::make($data, [
    'full-name' => 'required|min:5|max:70', 
    'address' => 'required'     
  ], $messages);

  return $validator;
}

Use in controller like this

$validator = $this->user_registration_rules($request_data);   
if($validator->fails())
{
  return redirect()->back()->withErrors($validator)->withInput();
}
like image 78
Komal Avatar answered Sep 30 '22 19:09

Komal