Spi*_*dey 3 php forms login laravel
从Laravel 5.2附带的默认auth和register sistem,Just做了一个简单的视图,可以是欢迎页面,并包含登录和注册表单.此时,如果按下任何按钮,则验证将检查并查找两个表单中的错误,并突出显示所有这些表单中所需的错误.那么包含它们的最佳解决方案是什么,也许每个都有单独的验证?
我试图将默认登录系统更改为与寄存器表单输入名称不同,但我认为有一个链,由于验证并将输入值添加到数据库中.
<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>
Run Code Online (Sandbox Code Playgroud)
可以看出,只需将电子邮件和密码输入名称从"email"更改为"login_email",将"password"更改为"login_password"
欢迎任何快速有效的解决方案/想法
小智 6
而不是更改输入名称,只需覆盖特征函数并从覆盖函数中调用它...
完成此操作后,我们可以存储一个会话值,告诉我们auth尝试来自登录或注册表单的位置!
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);
}
Run Code Online (Sandbox Code Playgroud)
并在您的View.blade文件中使用您的会话值检查您的错误...
<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>
Run Code Online (Sandbox Code Playgroud)
使用此功能,您可以检查按下的按钮是否来自登录ou注册按钮