Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
580 views
in Technique[技术] by (71.8m points)

php - Laravel login and register forms on the same page in Laravel 5.2

Login and Register forms on the same page

From default auth and register sistem that comes with Laravel 5.2, Just made a simple view, that could be the welcome page, and to include the login and register forms. At the moment, if any button is pressed, the validation checks and finds errors from both forms, and are highlighted all of them which are required. So what is the best solution to include them and maybe a separate validation for each?

I tried to change the default login sistem to be different from the register form input names, but i think there is a chain, due to validation and adding input values into database.

<div class="form-group{{ $errors->has('login_email') ? ' has-error' : '' }}">
    <label class="col-md-4 control-label">E-Mail Address</label>
    <div class="col-md-6">
         <input type="email" class="form-control" name="login_email" value="{{ old('login_email') }}">
         @if ($errors->has('login_email'))
              <span class="help-block">
                   <strong>{{ $errors->first('login_email') }}</strong>
              </span>
         @endif
     </div>
</div>

<div class="form-group{{ $errors->has('login_password') ? ' has-error' : '' }}">
     <label class="col-md-4 control-label">Password</label>
          <div class="col-md-6">
                <input type="password" class="form-control" name="login_password">
                @if ($errors->has('login_password'))
                     <span class="help-block">
                          <strong>{{ $errors->first('login_password') }}</strong>
                     </span>
               @endif
          </div>
      </div>

As it can be seen, just changed the email and password input names from 'email' to 'login_email' and from 'password' to 'login_password'

Any quick and efficient solutions/ideas are welcome

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead of changing input names, just Override trait function and call it from the overriden function...

With this done, we can store a session value that tell us from where the auth attempt is coming from login or register form!

    use AuthenticatesUsers, RegistersUsers {
    AuthenticatesUsers::redirectPath insteadof RegistersUsers;
    AuthenticatesUsers::getGuard insteadof RegistersUsers;
    login as traitLogin;
    register as traitRegister;
}

// Override trait function and call it from the overriden function
public function login(Request $request)
{
    //Set session as 'login'
    Session::put('last_auth_attempt', 'login');
    //The trait is not a class. You can't access its members directly.
    return $this->traitLogin($request);
}


public function register(Request $request)
{
    //Set session as 'register'
    Session::put('last_auth_attempt', 'register');
    //The trait is not a class. You can't access its members directly.
    return $this->traitRegister($request);
}

and in your View.blade file just check your errors with your Session value ...

<div class="form-group{{ $errors->has('email') && Session::get('last_auth_attempt') == 'login' ? ' 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') }}">

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

With this you can check if button pressed is from login ou register button


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...