B L Praveen
B L Praveen

Reputation: 1990

Two Form on the same page Laravel

I have register and singin with email field on the same page.If I submit register forum with validation errors the vaidation will be displayed on singin form as well.

How to sperate valdation to the register and signin form.

<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

Authentication is done on RegisterController

protected function create(array $data)
{   

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

Upvotes: 0

Views: 184

Answers (1)

Mozammil
Mozammil

Reputation: 8750

Named Error Bags should do the trick.

You should override the sendFailedLoginResponse() method in your LoginController.

/**
* 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');
}

... and then on the blade, you may have something like this:

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

Similarly, for the RegisterController, you should override your register() method.

/**
 * 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());
}

... and then on your blade

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

Upvotes: 1

Related Questions