我正在尝试用我的 PHP 代码解决“一页上有两个表单”的问题,但事实证明它比我预期的更棘手,而且表现得不像我想象的那样正确。
对于第一个表单(登录),我使用此 if 语句来确定消息是否用于登录。
@if(Session::has('message') && Session::get('last_message_for') == 'login')
<div class="notification is-{{ Session::get('color') }}">
<i class="fa fa-times"></i> {{ Session::get('message') }}
</div>
@elseif($errors->first() && Session::get('last_message_for') == 'login')
<div class="notification is-warning">
<i class="fa fa-times"></i> {{ $errors->first() }}
</div>
@endif
我的第二种形式有相同的代码,但它只是检查last_message_for
为“登录”提供不同的值。
@if(Session::has('message') && Session::get('last_message_for') == 'modal')
<div class="modal is-active" id="modal-forgotPassword">
@else
<div class="modal" id="modal-forgotPassword">
@endif
<div class="modal-background"></div>
<div class="modal-card">
<header class="modal-card-head">
<p class="modal-card-title" id="open-modal">Forgot Password?</p> <button class="delete"></button>
</header>
<form action="{{ route('frontend.guest.password.forgot') }}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<section class="modal-card-body">
<div class="content">
@if(Session::has('message') && Session::get('last_message_for') == 'modal')
<div class="notification is-{{ Session::get('color') }}">
<i class="fa fa-times"></i> {{ Session::get('message') }}
</div>
@endif
<div class="field">
<p class="control has-icons-left">
<input class="input" name="email" placeholder="Enter an email..." type="email">
<span class="icon is-small is-left"><i class="fa fa-envelope"></i></span>
</p>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</div>
</section>
<footer class="modal-card-foot">
<button class="button is-success" type="submit"><i class="fa fa-sign-in"></i> Send email</button>
</footer>
</form>
</div>
</div>
现在讨论问题,登录部分工作得很好,并且在有错误消息时显示错误消息,但第二个部分在有错误消息时没有显示任何错误消息。
我用它来设置last_message_for
Session::put('last_message_for', 'login');
这是我的第二种形式的代码:
public function onForgotPassword(Request $request) {
$validator = Validator::make($request->all(), [
'email' => 'required|email|exists:users,mail',
]);
Session::put('last_message_for', 'modal');
if ( $validator->fails()) {
return redirect()->route('frontend.guest.login')->withErrors($validator->messages());;
}
else {
Mail::to($request->input('email'))->send(new ForgotPasswordEmail());
return redirect()->route('frontend.guest.login')->withMessage('Email Sent')->withColor('warning');
}
}