Javascript 如果未选中任何复选框,则显示错误消息-Laravel

Javascript 如果未选中任何复选框,则显示错误消息-Laravel,javascript,php,html,laravel,checkbox,Javascript,Php,Html,Laravel,Checkbox,我尝试了几种方法来显示错误消息。这个想法是用户可以为他的产品添加标签。在我的主视图中,有一张表列出了所有产品。表格的每一行都有自己的复选框,用户可以选择自己想要的任何产品,在下一页为这些产品添加一些标签 我的问题是,我想显示一条闪光消息,告诉用户,他没有选中任何复选框。再也没有了。目前,他被引导到下一页,没有选择任何产品 我的尝试 这就是控制器的功能,如果用户提交表单,就会被引导到它 公共功能编辑() { } 在我看来: @if (Session::has('danger')) <

我尝试了几种方法来显示错误消息。这个想法是用户可以为他的产品添加标签。在我的主视图中,有一张表列出了所有产品。表格的每一行都有自己的复选框,用户可以选择自己想要的任何产品,在下一页为这些产品添加一些标签

我的问题是,我想显示一条闪光消息,告诉用户,他没有选中任何复选框。再也没有了。目前,他被引导到下一页,没有选择任何产品


我的尝试 这就是控制器的功能,如果用户提交表单,就会被引导到它

公共功能编辑() {

}

在我看来:

@if (Session::has('danger'))
    <div class="alert alert-danger">{{ Session::get('danger') }}</div>
@endif
@if(Session::has('danger'))
{{Session::get('danger')}
@恩迪夫
那不太管用。用户会显示错误消息,但如果他一切正常,下一页也会显示此错误消息,并且标记请求不再起作用

因此,我需要另一种方法来检查用户是否选择了任何复选框,并告诉他,他需要至少选择一个复选框来继续添加标记

可能使用Javascript/Jquery解决方案或laravel中的其他方式


谢谢你抽出时间,我很抱歉我的英语不好

我不会像那样手动验证输入,而是使用
validate()
方法

public function edit(Request $request, $id)
{
    $this->validate($request, [
        'title'      => 'required|string'
        'mycheckbox' => 'accepted'
    ]);

    // if user passes the validation above, the code here will be executed
    // Otherwise user will be redirected back to previous view with old input and validation errors.
    return view('my-view');
}
在您看来,您可以得到如下错误:

@if (session()->has('title'))
    <div class="alert alert-danger">{{ session()->first('mycheckbox') }}  </div>
@endif

@if (session()->has('mycheckbox'))
    <div class="alert alert-danger">{{ session()->first('mycheckbox') }}  </div>
@endif
@if(session()->has('title'))
{{session()->first('mycheckbox')}
@恩迪夫
@如果(session()->has('mycheckbox'))
{{session()->first('mycheckbox')}
@恩迪夫

阅读并查看另一个示例。

我将使用
validate()
方法,而不是像那样手动验证输入

public function edit(Request $request, $id)
{
    $this->validate($request, [
        'title'      => 'required|string'
        'mycheckbox' => 'accepted'
    ]);

    // if user passes the validation above, the code here will be executed
    // Otherwise user will be redirected back to previous view with old input and validation errors.
    return view('my-view');
}
在您看来,您可以得到如下错误:

@if (session()->has('title'))
    <div class="alert alert-danger">{{ session()->first('mycheckbox') }}  </div>
@endif

@if (session()->has('mycheckbox'))
    <div class="alert alert-danger">{{ session()->first('mycheckbox') }}  </div>
@endif
@if(session()->has('title'))
{{session()->first('mycheckbox')}
@恩迪夫
@如果(session()->has('mycheckbox'))
{{session()->first('mycheckbox')}
@恩迪夫

阅读并查看其他示例。

本例中的标题是什么?这只是一个示例。本例中的标题是什么?这只是一个示例。