Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Laravel validation messages

I'm trying to create customized messages for validation in Laravel 5. Here is what I have tried so far:

$messages = [     'required'  => 'Harap bagian :attribute di isi.',     'unique'    => ':attribute sudah digunakan', ]; $validator = Validator::make($request->all(), [     'username' => array('required','unique:Userlogin,username'),     'password' => 'required',     'email'    => array('required','unique:Userlogin,email'),$messages ]);  if ($validator->fails()) {      return redirect('/')         ->withErrors($validator) // send back all errors to the login form         ->withInput(); } else {     return redirect('/')         ->with('status', 'Kami sudah mengirimkan email, silahkan di konfirmasi');    }    

But it's not working. The message is still the same as the default one. How can I fix this, so that I can use my custom messages?

like image 820
YVS1102 Avatar asked Jul 10 '17 09:07

YVS1102


People also ask

What is the method used for specifying custom message for validation errors in form request?

After checking if the request failed to pass validation, you may use the withErrors method to flash the error messages to the session. When using this method, the $errors variable will automatically be shared with your views after redirection, allowing you to easily display them back to the user.

How do you validate exact words in laravel?

I know of at least two ways. // option one: 'in' takes a comma-separated list of acceptable values $rules = [ 'field' => 'in:hello', ]; // option two: write a matching regular expression $rules = [ 'field' => 'regex:^hello$', ];


1 Answers

Laravel 5.7.*

Also You can try something like this. For me is the easiest way to make custom messages in methods when you want to validate requests:

public function store() {     request()->validate([         'file' => 'required',         'type' => 'required'     ],     [         'file.required' => 'You have to choose the file!',         'type.required' => 'You have to choose type of the file!'     ]); } 
like image 111
Jankyz Avatar answered Sep 21 '22 11:09

Jankyz