如何使Laravel“确认"验证器将错误添加到确认字段? [英] How to make Laravel 'confirmed' validator to add errors to the confirmation field?

查看:140
本文介绍了如何使Laravel“确认"验证器将错误添加到确认字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,Laravel已确认"验证器会将错误消息添加到原始字段,而不是通常包含确认值的字段中.

By default, Laravel 'confirmed' validator adds the error message to the original field and not to the field which usually contains the confirmed value.

'password' => 'required|confirmed|min:8',

是否有任何简单的方法来扩展验证器或使用一些技巧来强制其始终在确认字段而不是原始字段上显示错误?

如果我两次输入密码失败,则该错误似乎更适合属于确认字段而不是原始密码字段.也许这只是我们的用户体验分析师变得挑剔...

If I fail to enter my password twice, the error seems more appropriate to belong the confirmation field and not to the original password field. Or maybe that's just our UX analyst getting nitpicky...

推荐答案

一种解决方法是使用same规则而不是confirmed

One way to go about it is to use same rule instead of confirmed

// ...

$input = Input::all();

$rules = [
    'password' => 'required|min:8',
    'password_confirmation' => 'required|min:8|same:password',
];

$messages = [
    'password_confirmation.same' => 'Password Confirmation should match the Password',
];
$validator = Validator::make($input, $rules, $messages);

if ($validator->fails()) {
    return back()->withInput()->withErrors($validator->messages());
}
// ...

这篇关于如何使Laravel“确认"验证器将错误添加到确认字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆