【问题标题】:Laravel Flash errors - How to check if a checkbox is checked?Laravel Flash 错误 - 如何检查复选框是否被选中?
【发布时间】:2017-05-02 19:31:33
【问题描述】:

我正在尝试在 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

这是我的删除约会之刃

@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

【问题讨论】:

    标签: laravel laravel-5


    【解决方案1】:

    你可以试试这个

    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');
    }
    

    【讨论】:

    猜你喜欢
    • 2019-01-12
    • 2016-05-11
    • 2021-07-16
    相关资源
    最近更新 更多