【问题标题】:Two Form on the same page Laravel同一页面上的两个表单 Laravel
【发布时间】:2019-01-28 13:31:10
【问题描述】:

我已在同一页面上使用电子邮件字段注册和登录。如果我提交注册论坛时出现验证错误,则验证也会显示在登录表单上。

如何对注册和登录表单进行验证。

<input id="email" class="" name="email" type="text" placeholder="Email">                         
@if ($errors->has('email'))
    <span class="invalid-feedback" role="alert">
        <strong>{{ $errors->first('email') }}</strong>
    </span>
@endif

在 RegisterController 上完成身份验证

protected function create(array $data)
{   

    return User::create([
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);
}           

【问题讨论】:

  • 你能连接你的控制器吗?
  • @Mcfaith 指向 RegisterController

标签: laravel


【解决方案1】:

Named Error Bags 应该可以解决问题。

您应该覆盖 LoginController 中的 sendFailedLoginResponse() 方法。

/**
* Get the failed login response instance.
*
* @param  \Illuminate\Http\Request  $request
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Illuminate\Validation\ValidationException
*/
protected function sendFailedLoginResponse(Request $request)
{
    return back()
        ->withInput($request->only($this->username(), 'remember'))
        ->withErrors([
            $this->username() => [trans('auth.failed')],
        ], 'login');
}

...然后在刀片上,你可能会有这样的东西:

@if($errors->login->has('email'))
    <span class="help-block">
        <strong>{{ $errors->login->first('email') }}</strong>
    </span>
@endif

同样,对于 RegisterController,您应该覆盖您的 register() 方法。

/**
 * Handle a registration request for the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function register(Request $request)
{
    $validator = $this->validator($request->all());

    if ($validator->fails()) {
        return back()
            ->withErrors($validator, 'register');
    }

    event(new Registered($user = $this->create($request->all())));

    $this->guard()->login($user);

    return $this->registered($request, $user)
                    ?: redirect($this->redirectPath());
}

...然后在你的刀片上

@if($errors->register->has('email'))
    <span class="help-block">
        <strong>{{ $errors->register->first('email') }}</strong>
    </span>
@endif

【讨论】:

  • @Mozammil 这会影响 $errors->first('email')。因为我有其他使用 $errors->first('email') 的表单。我需要到处更改吗
  • 不,它只会影响您的登录/注册刀片 :)
猜你喜欢
  • 2014-03-23
  • 1970-01-01
  • 1970-01-01
  • 2013-06-24
  • 2015-05-13
  • 2012-07-19
  • 2014-07-22
  • 1970-01-01
  • 2012-12-13
相关资源
最近更新 更多