www.WarungProgram.com

Tempat Belajar Bahasa Pemrograman Dari Dasar


Cara Custom Validation Message Laravel 10

Penulis : Zulfikar Kategori : Uncategorized

Berikut ini cara untuk custom Validation Message pada Controller di Laravel 10 :

  • Import Validator dengan cara tambahkan baris use Illuminate\Support\Facades\Validator;
  • Tambahkan Custom Message Validation pada parameter array ke tiga.
  • Jika ingin custom nama attribute gunakan method $validator->setAttributeNames();

App/Http/Controllers/Login.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class Login extends Controller {

    public function index(Request $request) {

        $validator = Validator::make($request->all(), [
            'username' => 'required|min:3',
            'password' => 'required|min:6'
        ], [
            'required' => ':attribute tidak boleh kosong!',
            'min' => ':attribute minimal :min karakter!'
        ]);

        $validator->setAttributeNames(
            [
                'username' => 'Username',
                'password' => 'Password'
            ]
        );

        if ($validator->fails()) {
            return response()->json(['sts' => false, 'msg' => $validator->errors()], 400);
        }

        return response()->json(['sts' => true, 'msg' => 'sukses']);
    }
}


Warning: Undefined variable $aria_req in /home/warungprogram/public_html/wp-content/themes/vikar/comments.php on line 51

Warning: Undefined variable $aria_req in /home/warungprogram/public_html/wp-content/themes/vikar/comments.php on line 56
*
*