Laravel 5.2 - 验证:显示自定义错误信息 [英] Laravel 5.2 - Auth: display custom error messages

查看:971
本文介绍了Laravel 5.2 - 验证:显示自定义错误信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何自定义错误消息(如这些证书不符合我们的记录。),它们无需触碰后未成功登录/注册显示基础文件?我正在寻找一个解决方案,并希望优雅的一个,至少不碰 AuthenticatesAndRegistersUsers 也不 ThrottlesLogins :)

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 :)

我使用了 AuthController 和执行后Laravel提供形式:

I'm using the AuthController and forms provided by Laravel after executing:

PHP工匠化妆:AUTH

php artisan make:auth

控制器:

(它只有一个构造函数和两个方法,其余落在基础,方法是:)

(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 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>

感谢您!

推荐答案

您可以覆盖 getFailedLoginMessage AuthController 它来源于 AuthenticatesUsers 特性

You can override getFailedLoginMessage on the AuthController which comes from the AuthenticatesUsers trait

protected function getFailedLoginMessage()
{
    return 'what you want here.';
}

还是不覆盖它,并为 auth.failed A浪值。在 getFailedLoginMessage 方法将检查郎::已经('auth.failed')和使用,如果其可用。

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.

有关实际的校验错误消息可以覆盖 postLogin ,并通过自己的消息的数组验证,或者如果你想全局更改他们,你可以在相应的文件郎调整它们在资源/郎/ {}郎/validation.php

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.

这篇关于Laravel 5.2 - 验证:显示自定义错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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