php Laravel 5.2 - Auth:显示自定义错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34750675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.2 - Auth: display custom error messages
提问by Juan Serrats
How can I customize the error messages (such as "These credentials do not match our records."
) that are displayed upon unsuccessful login/registration without having to touch the foundation files? I'm looking for a solution and hopefully an elegant one, at least not touching AuthenticatesAndRegistersUsers
nor ThrottlesLogins
:)
如何自定义在"These credentials do not match our records."
登录/注册失败时显示的错误消息(例如),而无需接触基础文件?我正在寻找一个解决方案,希望是一个优雅的解决方案,至少不接触AuthenticatesAndRegistersUsers
也不ThrottlesLogins
:)
I'm using the AuthController
and forms provided by Laravel after executing:
执行后我正在使用AuthController
Laravel 提供的和 表单:
php artisan make:auth
php artisan make:auth
Controller:
控制器:
(it only has a constructor and two methods, the rest falls on the foundation, the methods are:)
(它只有一个构造函数和两个方法,剩下的就靠基础了,方法是:)
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
Form:
形式:
<form class="form-horizontal" role="form" method="POST" action="{{ url('/login') }}">
{!! csrf_field() !!}
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">E-Mail</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="{{ old('email') }}" required>
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password" required>
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<label>
<input type="checkbox" name="remember"> Remember Me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
<i class="fa fa-btn fa-sign-in"></i>Login
</button>
<a class="btn btn-link" href="{{ url('/password/reset') }}">Forgot Your Password?</a>
</div>
</div>
Thank you!
谢谢!
回答by lagbox
You can override getFailedLoginMessage
on the AuthController
which comes from the AuthenticatesUsers
trait
您可以覆盖getFailedLoginMessage
在AuthController
它来源于AuthenticatesUsers
特质
protected function getFailedLoginMessage()
{
return 'what you want here.';
}
Or not override it and set a lang value for auth.failed
. The getFailedLoginMessage
method will check for Lang::has('auth.failed')
and use that if its available.
或者不覆盖它并为auth.failed
. 如果可用,该getFailedLoginMessage
方法将检查Lang::has('auth.failed')
并使用它。
For the actual validation error messages you can override the postLogin
and pass your own array of messages to validate
, or if you wanted to change them globally you can adjust them in the appropriate lang file in resources/lang/{lang}/validation.php
.
对于实际的验证错误消息,您可以覆盖postLogin
并将您自己的消息数组传递给validate
,或者如果您想全局更改它们,您可以在resources/lang/{lang}/validation.php
.
回答by Joshua Hutchison
You don't want to override the getFailedLoginMessage() method in AuthController. The proper solution is to change the message in the designed location. If you look in the Resources > lang > en folder, you will see an auth.php file. In it, there is a "failed" attribute with a message you can customize. Change it there. The orignial getFailedLoginMessage() method in the Laravel auth files in vendor looks to that location for a custom message first, before settling on the default.
您不想覆盖 AuthController 中的 getFailedLoginMessage() 方法。正确的解决方案是在设计的位置更改消息。如果您查看 Resources > lang > en 文件夹,您将看到一个 auth.php 文件。其中,有一个“失败”属性,您可以自定义一条消息。在那里换。供应商中 Laravel 身份验证文件中的原始 getFailedLoginMessage() 方法首先在该位置查找自定义消息,然后再确定默认值。