从 Laravel 5.2 自带的默认 auth 和 register 系统, 只是制作了一个简单的 View ,可以是欢迎页面,并包括登录和注册表格。 目前,如果按下任何按钮,验证会检查并发现两种形式的错误,并突出显示所有需要的错误。 那么将它们包括在内的最佳解决方案是什么,并且可能对每个单独验证?
我试图将默认登录系统更改为与注册表单输入名称不同,但我认为存在一个链,因为要进行验证并将输入值添加到数据库中。
<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>
可以看出,只是将电子邮件和密码输入名称从“email”更改为“login_email”,从“password”更改为“login_password”
欢迎任何快速有效的解决方案/想法
最佳答案
无需更改输入名称,只需覆盖特征函数并从覆盖的函数中调用它...
完成此操作后,我们可以存储一个 session 值,告诉我们身份验证尝试来自登录或注册表单的来源!
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);
}
并在您的 View.blade 文件中检查您的 session 值错误...
<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>
用这个你可以检查按下的按钮是否来自登录或注册按钮
关于php - Laravel 5.2 中同一页面上的 Laravel 登录和注册表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35097071/