我尝试了一些方法来显示错误消息。这个想法是用户可以为他的产品添加标签。在我的主视图上是一张列出了所有产品的表格。表格的每一行都有自己的复选框,用户可以选择他想要的任何产品,在下一页上为这些产品添加一些标签。
我的问题是,我想显示一个闪光消息,告诉用户,他没有勾选任何复选框。没有别的了。目前,他被引导到下一个页面,没有选择任何产品。
我的尝试
这是控制器的功能,如果用户提交表单,他将被引导到
公共函数edit(){
// some non important controller code
if(count(Input::get('id')) == 0)
{
Session::flash('danger', 'Sie haben kein Produkt gewählt!');
return redirect()->back();
}
else
{
return view('layout.edit.productedit', [
'ids' => $data, // non important variables
'products' => $product_name
]);
}
}
在我看来:
@if (Session::has('danger'))
<div class="alert alert-danger">{{ Session::get('danger') }}</div>
@endif
这样做效果不太好。用户会显示他的错误消息,但如果他做得很好,下一页也会收到这个错误消息,并且标记请求不再工作。
因此,我需要另一种方法来检查用户是否选择了任何复选框,并告诉他,他需要至少选择一个复选框来继续添加标签。
也许可以使用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
阅读官方文档,我将看到其他示例。