在 Array Bag 中设置自定义错误消息
Setting custom error message in Array Bag
如果不满足某个条件,我想重定向查看。校验码:
if($request->get('files') == 'yes' && $request->file('file_name') == null){
return Redirect::to('brief/'.$id."/edit")
->withErrors('errors',"Mention file")
->withInput();
}
它将数组打印为:
Illuminate\Support\ViewErrorBag {#239
#bags: array:1 [
"Mention file" => Illuminate\Support\MessageBag {#240
#messages: array:1 [
0 => array:1 [
0 => "errors"
]
]
#format: ":message"
}
]
在查看结束时我正在做:
@if (count($errors) > 0)
<div style="margin-top: 10%;" class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
不打印消息提及文件
好的,我发现了问题。
来自文档:
you may use the withErrors method to flash the error messages to the
session. When using this method, the $errors variable will
automatically be shared with your views after redirection,
所以 ->withErrors('errors',"Mention file")
实际上是将 $errors
转换成一个字符串,这把事情搞砸了。简单地做 ->withErrors("Mention file")
只是在 ErrorBag
中添加了消息。所以正确的代码应该是:
if($request->get('files') == 'yes' && $request->file('file_name') == null){
return Redirect::to('brief/'.$id."/edit")
->withErrors("Mention file")
->withInput();
}
要么使用 ->withErrors
传递字符串,要么使用 ->with
:
传递具有键 "errors" 的数组
if($request->get('files') == 'yes' && $request->file('file_name') == null){
return Redirect::to('brief/'.$id."/edit")
->withErrors("Mention file")
->withInput();
}
或
if($request->get('files') == 'yes' && $request->file('file_name') == null){
return Redirect::to('brief/'.$id."/edit")
->with(["errors" => "Mention file"])
->withInput();
}
如果不满足某个条件,我想重定向查看。校验码:
if($request->get('files') == 'yes' && $request->file('file_name') == null){
return Redirect::to('brief/'.$id."/edit")
->withErrors('errors',"Mention file")
->withInput();
}
它将数组打印为:
Illuminate\Support\ViewErrorBag {#239
#bags: array:1 [
"Mention file" => Illuminate\Support\MessageBag {#240
#messages: array:1 [
0 => array:1 [
0 => "errors"
]
]
#format: ":message"
}
]
在查看结束时我正在做:
@if (count($errors) > 0)
<div style="margin-top: 10%;" class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
不打印消息提及文件
好的,我发现了问题。
来自文档:
you may use the withErrors method to flash the error messages to the session. When using this method, the $errors variable will automatically be shared with your views after redirection,
所以 ->withErrors('errors',"Mention file")
实际上是将 $errors
转换成一个字符串,这把事情搞砸了。简单地做 ->withErrors("Mention file")
只是在 ErrorBag
中添加了消息。所以正确的代码应该是:
if($request->get('files') == 'yes' && $request->file('file_name') == null){
return Redirect::to('brief/'.$id."/edit")
->withErrors("Mention file")
->withInput();
}
要么使用 ->withErrors
传递字符串,要么使用 ->with
:
if($request->get('files') == 'yes' && $request->file('file_name') == null){
return Redirect::to('brief/'.$id."/edit")
->withErrors("Mention file")
->withInput();
}
或
if($request->get('files') == 'yes' && $request->file('file_name') == null){
return Redirect::to('brief/'.$id."/edit")
->with(["errors" => "Mention file"])
->withInput();
}