我正在尝试在Laravel中执行flash消息,现在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
答案 0 :(得分: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');
}