Laravel 5.4在注册失败期间命名为错误袋 [英] Laravel 5.4 Named Error Bag during registration failure

查看:47
本文介绍了Laravel 5.4在注册失败期间命名为错误袋的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Laravel内置的身份验证( php artisan make:auth ),我在同一页面上同时显示注册和登录表单.但是,由于两种形式都具有相同的字段(电子邮件和密码),因此一种形式的错误也会在另一种形式上显示-即,当您使用收到的电子邮件进行注册或使用不正确的电子邮件进行登录时.

我更改了命名错误包( https://laravel.com/docs/5.4/validation#named-error-bags )作为登录表单,方法是将 sendFailedLoginResponse AuthenticatesUsers.php 复制到我的 LoginController.php ,并进行以下更改:

-> withErrors($ errors);

-> withErrors($ errors,'login');

并且还更改了我的登录刀片错误输出:

$ errors-> has()

$ errors-> login-> has()

但是,我正在尝试为注册期间的验证失败更改命名的错误包,但是努力寻找在哪里或如何执行此操作.似乎失败的注册永远不会使它超出 RegistersUsers.php 中的以下各项,但是我无法跟踪可以扩展的位置/位置并添加 withErrors :

$ this-> validator($ request-> all())-> validate();

我知道还有另一种方法,可以在某个地方设置 protected $ errorBag ='register'; ,但是我无法确定在哪里.

任何帮助将不胜感激,因为我才刚刚开始使用Laravel.

解决方案

我在register函数上遇到了同样的问题,最终我在我的 registerController 中覆盖了register函数,并尝试进行捕获到 $ this-> validator($ request-> all())-> validate .所以现在看起来像

  try {$ this-> validator($ request-> all())-> validate();} catch(ValidationException $ e){$ e-> errorBag('register');抛出$ e;} 

它似乎像一种魅力!(我在laravel 5.6中工作,但我确定它会在laravel 5.4中工作)

Using Laravel's built in Authentication (php artisan make:auth) I'm displaying both the registration and login form on the same page. However, because both forms have the same fields (email and password), the error for one form is also shown on the other - ie when you register with an email that's taken, or login with an incorrect email.

I've changed the named error bag (https://laravel.com/docs/5.4/validation#named-error-bags) for the login form, by copying sendFailedLoginResponse from AuthenticatesUsers.php to my LoginController.php, and changing:

->withErrors($errors);

to

->withErrors($errors, 'login');

And also changing my login blade error output from:

$errors->has()

to

$errors->login->has()

However, I'm trying to change the named error bag for the validation failure during registration, but struggling to find where or how I can do this. It seems that a failed registration never makes it past the following in RegistersUsers.php, but I can't track down what / where I can extend and add withErrors:

$this->validator($request->all())->validate();

I understand there is another way to do this, by setting protected $errorBag = 'register'; somewhere, but I can't work out where.

Any help would be appreciated, as I've only just started to use Laravel.

解决方案

I had this same issue with the register function, and i ended up overwriting the register function in my registerController and placed a try catch to $this->validator($request->all())->validate. So now it looks like

try {
    $this->validator($request->all())->validate();
} catch (ValidationException $e) {
    $e->errorBag('register');

    throw $e;
}

and it seems to work like a charm! (I was working in laravel 5.6 but i'm sure it'll work in laravel 5.4)

这篇关于Laravel 5.4在注册失败期间命名为错误袋的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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