Home > Net >  custom validation message in laravel 8
custom validation message in laravel 8

Time:03-21

how to give custom laravel message in my coding i am new in laravel

$validatedData = $r->validate([
            'preference' => 'required',
            'objective_1' => 'required',
            'objective_2' => 'required',
            'objective_5' => 'required',
        ]);
        if (empty($r->session()->get('sessForm'))) {
            $r->session()->put('sessForm', $validatedData);
        } else {
            $sessForm = $r->session()->get('sessForm');
            $r->session()->put('sessForm', $sessForm);
        }
  

CodePudding user response:

You can use custom validator. Check the documentation. You can get messages from $errors variable in blade template.

$validator = Validator::make($request->all(), [
    'title' => 'required|unique:posts|max:255',
    'body' => 'required',
], [
    'title.required' => 'Title required custom message',
    'title.unique' => 'Title unique custom message',
    // ...
]);

if ($validator->fails()) {
    return redirect()->back()
                ->withErrors($validator)
                ->withInput();
}

If you want to send redirect with data. You can use

redirect()->route('route')->with('data', 'test');

You can get after redirect

session('data')
  • Related