2017-05-02 138 views
0

我正在嘗試在Laravel中執行Flash消息,現在這些Flash消息在沒有複選框的頁面上適用於成功消息和錯誤消息。Laravel Flash錯誤 - 如何檢查複選框是否被選中?

我有一個名爲'deleteappointmentform'的視圖,它要求用戶選中一個複選框,並刪除所選的約會,但是如果我沒有選中任何複選框並單擊提交,它會給我一個成功消息,而不會實際檢查他們是否已勾選和複選框。我試圖得到它顯示一個錯誤信息,如果他們不檢查任何複選框

任何幫助的感謝,謝謝

這是與刪除約會

function deleteAppointment(Request $request) 
{ 
    Appointment::destroy($request->appointments); 
    Session::flash('successCancelAppointment', 'Appointment cancelled successfully!'); 
return redirect('all'); 
} 

此交易的功能是我的消息刀片

@if (Session::has('successCancelAppointment')) 

<div class="alert alert-success" role="alert"> 
    <strong>Success: </strong> {{Session::get('successCancelAppointment')}} 
</div> 
    @endif 



@if (count($errors) > 0) 

<div class="alert alert-danger" role="alert"> 
    <strong>Errors:</strong> 
    <ul> 
    @foreach ($errors->all() as $error) 
     <li>{{$error}}</li> 
     @endforeach 

    </ul> 
</div> 
@endif 

這是我deleteappointmentsblade

@extends('layouts.master') 
@section('title', 'Cancel Appointment') 
@section('content') 

<form action="{{url('deleteappointment')}}" method="POST"> 
    {{ csrf_field() }} 
    @foreach ($appointments as $appointment) 
     <div> 
      <label> {{$appointment->user->firstname}} {{$appointment->user->surname}}</label> 
      <label>Has an appointment at: {{$appointment->time}}</label> 
      <label>On: {{$appointment->date}}</label> 
      <label>With Dr: {{$appointment->doctor->surname}}</label> 

      <input type='checkbox' value='{{$appointment->id}}' name='appointments[]'/> 
     </div> 
    @endforeach 
    <input type="submit" name="submitBtn" value="Cancel Appointments"> 
</form> 


@endsection 

回答

2

你可以試試這個

function deleteAppointment(Request $request) 
{  $rules=array(
      'appointments'=>'required' 
     ); 
     $validator = Validator::make($request->all(), $rules); 
     if($validator->fails()) 
     { 
     $messages = $validator->messages(); 
     $errors = $messages->all(); 
     return redirect()->back()->withErrors($errors); 
     } 
     Appointment::destroy($request->appointments); 
    Session::flash('successCancelAppointment', 'Appointment cancelled 
     successfully!'); 
    return redirect('all'); 
} 
+0

那做工精良,三江源,請您能否簡要解釋代碼的規則和驗證部分是幹什麼的? – TF120

+0

請參閱此鏈接https://laravel.com/docs/5.4/validation#form-request-validation –

相關問題