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?

推荐答案

If you use $this->validate() simplest one, then you should write code something like this..

$rules = [
        'name' => 'required',
        'email' => 'required|email',
        'message' => 'required|max:250',
    ];

    $customMessages = [
        'required' => 'The :attribute field is required.'
    ];

    $this->validate($request, $rules, $customMessages);

Laravel相关问答推荐

当未通过验证的字段是值数组时,Laravel$validator->;validator()方法不会引发异常

AJAX响应中未定义的全名

Inertiajs - 使用链接组件添加记录但无法在不使用表单的情况下清除文本区域

为什么使用 created_at 表在迁移错误中创建表?

Laravel 验证 - 不同的属性规范

Laravel 5 - 更改模型文件位置

指令allow_call_time_pass_reference警告

Eloquent: hasNot 带参数

PHP Laravel:如何设置或获取会话数据?

查询构建器中的 Laravel 5 多重 Select ()

我如何从给定的日期时间 struct 创建Carbon 对象?

调整行数以形成:: Textarea Laravel 5

安装后在 Lumen 中找不到页面

改变模式生成器中的列长度?

如何从 Laravel 中的资源中获取图像?

使用 Laravel 使用 2 个磁盘复制文件

如何在 Laravel 5 中验证 RESTful API?

Laravel 5.2 验证错误未出现在Blade中

如何在 Laravel 5.3 中获取当前的语言环境

laravel:如何获取 eloquent 模型的列名?