英文:
Image form validation with Laravel and Vue
问题
I have an image field that should not be required, but should be limited to possible file types.
但是,如果我验证 MIME 类型,提交表单时没有图像是不起作用的。
request()->validate([
'name' => ['required'],
'type' => ['required'],
'master_id' => ['required'],
'description' => ['required'],
'whom_id' => ['required'],
'short_description' => ['required'],
'image' => ['mimes:jpg,bmp,png', 'max:3072'],
]);
if (request()->image) {
$ext = request()->file('image')->extension();
$path = request()->file('image')->storeAs('image', $course->id . 'mainImage.' . $ext);
$course->image = $path;
}
英文:
I have an image field that should not be required, but should be limited to possible file types
But, if I validate mimes, submitting the form without an image does not work
request()->validate([
'name' => ['required'],'name' => ['required'],
'type' => ['required'],'master_id' => ['required'],
'description' => ['required'],
'whom_id' => ['required'], 'short_description' => ['required'],
'image' => ['mimes:jpg,bmp,png','max:3072'],
]);
if(request()->image){
$ext = request()->file('image')->extension();//Получение расширения файла
$path = request()->file('image')->storeAs('image', $course->id.'mainImage.'.$ext);
$course->image = $path;
}
答案1
得分: 2
通常情况下,对于标准表单提交,答案是有时
,然而,您正在通过Vue进行提交,因此需要nullable
。
'image' => ['nullable', 'mimes:jpg,bmp,png', 'max:3072'],
请注意可选字段的说明。
英文:
Normally for standard form submits, the answer is sometimes
, however you're submitting by Vue, so it required nullable
.
'image' => ['nullable', 'mimes:jpg,bmp,png','max:3072'],
Mind the Note On Optional Fields.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论