我有一个Laravel程序,可以保存表格数据并上传一些图片。在验证中,有两个规则。图片是必需的,并且必须是图片类型(jpg,jpeg,png)。但是,验证仅检查文件类型,而不检查“必需”。即使没有图像,它也允许用户提交。为什么?
public function updateImages(Request $request, $id)
{
$validatedData = $request->validate([
'image' => 'required',
'image.*' => 'image|mimes:jpeg,png,jpg|max:2048',
],
[
'image.*.image' => 'Format Error: Please uplaod image in a png or jpg format',
]);
$item = Post::find($id);
$existing_count = Photo::where('post', $item->id)->count();
$countrequest = sizeof($request->file('image'));
$count = $existing_count + $countrequest;
if ($count >= 6) {
return back()
->withInput()
->withErrors(['Only 5 images can be uploaded!']);
}
$upload = $this->imageUpload($item, $request);
return redirect()->back()->with('message', 'Image Updated');
}
答案 0 :(得分:0)
通过image.*
申请要求。例如-
image.*' => 'require|image|mimes:jpeg,png,jpg|max:2048',
尝试此解决方案。它将起作用。
答案 1 :(得分:0)
您可以使用Laravel Request Validation
要创建表单请求类
class GzipFilterSpec
extends PlaySpec
with GuiceOneServerPerSuite
with FutureAwaits
with DefaultAwaitTimeout {
override def fakeApplication(): Application =
new GuiceApplicationBuilder()
.loadConfig(env => Configuration.load(env))
.build()
"Server" must {
"compress responses" in {
val wsClient: WSClient = app.injector.instanceOf[WSClient]
val appStatusUrl = s"http://localhost:$port/app_status"
val gzipResponse: WSResponse = await(
wsClient
.url(appStatusUrl)
.addHttpHeaders(ACCEPT_ENCODING -> "gzip")
.get())
val response: WSResponse = await(wsClient.url(appStatusUrl).get())
println(response.body)
println(gzipResponse.body)
checkGzippedBody(gzipResponse, response.body)
}
}
}
转到app / Http / Requests添加规则
php artisan make:request ImageUpdateRequest
在您的控制器上
public function authorize()
{
return true;
}
public function rules()
{
return [
'image' => 'required|image|mimes:jpeg,png,jpg|max:2048'
];
}