我试图在一个页面上解决一个"两种形式"我的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
是否有不同的值来登录&#39;。
@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');
}
}
答案 0 :(得分:1)
你没有处理验证错误,只处理返回的消息,这应该这样做。
@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>
@elseif($errors->first() && Session::get('last_message_for') == 'login')
<div class="notification is-warning">
<i class="fa fa-times"></i> {{ $errors->first() }}
</div>
@endif
答案 1 :(得分:0)
我在同一页面中有两个表单,因此您可以使用Flash会话轻松地将错误分配给每个表单 在您的控制器中只需使用像这样的会话
use Session;
此代码与两个搜索表单有关
//searchInHistory
public function searchInHistory(){
$date = Request()->all();
$rules = [
'dateFrom' =>'required',
'dateTo' =>'required',
];
$validator = Validator($date,$rules);
if ($validator->fails()){
Session::flash('inError', 'inError');
return redirect()
->back()
->withErrors($validator)
->withInput();
}else{
$store = DB::table('stores')->select(
'store_details.id',
'store_details.status',
'stores.id AS storeId',
'stores.partNo',
'stores.title',
'store_details.serialNo',
'store_details.created_at',
'store_details.updated_at'
)
->join('store_details', 'store_details.storeId', '=', 'stores.id')
->where('store_details.status','inside')
->whereBetween('store_details.created_at',[$date['dateFrom'],$date['dateTo']])
->get();
return view('crm.store.in',compact('store'));
}
}
//===============
//searchHistory
public function searchOutHistory(){
$date = Request()->all();
$rules = [
'dateFrom' =>'required',
'dateTo' =>'required',
];
$validator = Validator($date,$rules);
if ($validator->fails()){
Session::flash('inError', 'inError');
return redirect()
->back()
->withErrors($validator)
->withInput();
}else{
$store = DB::table('stores')->select(
'store_details.id',
'store_details.status',
'stores.id AS storeId',
'stores.partNo',
'stores.title',
'store_details.serialNo',
'store_details.created_at',
'store_details.updated_at'
)
->join('store_details', 'store_details.storeId', '=', 'stores.id')
->where('store_details.status','outside')
->whereBetween('store_details.updated_at',[$date['dateFrom'],$date['dateTo']])
->get();
return view('crm.store.out',compact('store'));
}
}
//===============
下面的代码将每个错误分配给使用会话的特定表单 这是与两种形式有关的刀片代码视图代码
<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-xs-12">
<div class="box">
<div class="box-header">
<h3 class="box-title">Store IN / OUT Control</h3>
</div>
<div class="row">
<div class="col-md-6">
<div class="box-body">
@if(session('outError'))
@if ($errors->any())
<div class="alert alert-danger">
<center>
@foreach ($errors->all() as $error)
{{ $error }}<br>
@endforeach
</center>
</div>
@endif
@endif
@if(session('out'))
@if(session('save'))
<div class="alert alert-success">
<center>
Products Came out of Successfully
</center>
</div>
@endif
@endif
<!-- form start -->
<form role="form" method="post" action="{{url('admin/takeProductOutStore')}}" enctype="multipart/form-data">
{{csrf_field()}}
<div class="box-body">
<input type="hidden" name="id" value="{{$storeId}}">
<div class="form-group">
<label>Products in Store</label>
<select multiple class="form-control" name="products[]" required>
@foreach($InDoorProducts as $row)
<option value="{{$row->serialNo}}">{{$row->serialNo}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label>Date</label>
<input type="date" class="form-control" name="date" required>
</div>
<div class="form-group">
<textarea class="textarea" required placeholder=" Write Notes"style="width: 100%; height: 125px; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;" name="note" value="{{old('note')}}"></textarea>
</div>
</div><!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-primary">Out Store <b>→</b></button>
</div>
</form>
</div>
</div>
<div class="col-md-6">
<div class="box-body">
@if(session('inError'))
@if ($errors->any())
<div class="alert alert-danger">
<center>
@foreach ($errors->all() as $error)
{{ $error }}<br>
@endforeach
</center>
</div>
@endif
@endif
@if(session('in'))
@if(session('save'))
<div class="alert alert-success">
<center>
Products Added to Store Again Successfully
</center>
</div>
@endif
@endif
<!-- form start -->
<form role="form" method="post" action="{{url('admin/takeProductInStore')}}" enctype="multipart/form-data">
{{csrf_field()}}
<div class="box-body">
<input type="hidden" name="id" value="{{$storeId}}">
<div class="form-group">
<label>Products Out Store</label>
<select multiple class="form-control" name="products[]" required>
@foreach($OutDoorProducts as $row)
<option>{{$row->serialNo}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label>Date</label>
<input type="date" class="form-control" name="date" required>
</div>
<div class="form-group">
<textarea class="textarea" required placeholder=" Write Notes"style="width: 100%; height: 125px; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;" name="note" value="{{old('note')}}"></textarea>
</div>
</div><!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-primary">Back To Store <b>←</b></button>
</div>
</form>
</div>
</div>
</div>
</section>