同一页上的两个窗体Laravel

时间:2019-01-28 05:09:46

标签: laravel

我在同一页面上使用电子邮件字段进行注册和注册。如果我提交带有验证错误的注册论坛,验证也将以singin形式显示。

如何对注册和登录表格进行评估。

<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']),
    ]);
}           

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:-1)

一种解决方法是更改​​字段名称。例如,您可以将字段名称从“ email”更改为“ register_email”。

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

还相应更改了控制器,

protected function create(array $data)
{   

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