首先,我遵循了Dropzone:数据与文件相结合的文档
我创建了一个自定义按钮来处理排队的文件,而不是表单的默认提交按钮。

这是我的Dropzone JS代码块:
// Dropzone
let token = $('meta[name="csrf-token"]').attr('content');
var myDropzone = new Dropzone("#report_attachments", {
url: storeReport, // Set the url for your upload script location
paramName: 'file',
maxFiles: 10,
maxFilesize: 10, // MB
addRemoveLinks: true,
autoProcessQueue: false,
autoQueue: true,
acceptedFiles: '.png, .jpg, .jpeg, .webp, .gif',
params: {
_token: token,
},
});这是HTML:
<!--begin::Input group-->
<!--begin::Dropzone-->
<div class="dropzone" id="report_attachments">
<!--begin::Message-->
<div class="dz-message needsclick">
<!--begin::Icon-->
<i class="bi bi-file-earmark-arrow-up text-primary fs-3x"></i>
<!--end::Icon-->
<!--begin::Info-->
<div class="ms-4">
<h3 class="dz-default fs-5 fw-bold text-gray-900 mb-1">Drop files here or click to upload.</h3>
<span class="fs-7 fw-semibold text-gray-400">Upload up to 10 files</span>
</div>
<!--end::Info-->
</div>
</div>
<!--end::Dropzone-->
<button type="button" class="btn btn-small btn-primary mt-3" id="upload_dropzone">
Process Files
</button>
<!--end::Input group-->侧注:我在使用表Validator和表单
现在让我们来讨论这个问题。
每次我试图处理排队的文件时,都会在laravel调试器中得到以下错误:

ir number实际上是表单中的一个字段,并且是填充的!实际上,表单中所有必需的字段都已被填充。当然,因为它是laravel,所以我有一个请求文件,它告诉需要哪个字段以及它是什么类型的字段:
public function rules()
{
return [
'ir_number' => 'required|integer',
'type' => 'required|integer',
'location' => 'required|integer',
'start_date_time' => 'required|date',
'end_date_time' => 'required|date',
'weather' => 'nullable|string',
'severity' => 'required|string',
'injury_source' => 'required|string',
'violator_full_name' => 'required|string',
'violator_id' => 'required|integer',
'violator_phone' => 'required|integer',
'violator_sex' => 'required|string',
'violator_age' => 'nullable|string',
'violator_nationality' => 'required|string',
'violator_type' => 'required|string',
'violator_injury_reason' => 'required|string',
'violator_involvement' => 'required|string',
'vehicle_owner_name' => 'nullable|string',
'vehicle_owner_id' => 'nullable|integer',
'vehicle_make' => 'nullable|string',
'vehicle_model' => 'nullable|string',
'vehicle_color' => 'nullable|string',
'plate_number' => 'nullable|string',
'vehicle_type' => 'nullable|string',
'description' => 'required|string',
'cause' => 'required|string',
'actions_taken' => 'required|string',
'details' => 'required|string',
'attachment' => 'nullable|image|mimes:jpeg,jpg,png,gif,webp',
'recommendation' => 'required|string',
'officer_name' => 'required|string',
'supervisor_name' => 'required|string',
'manager_name' => 'required|string',
'ccc_employee' => 'required|string',
'status' => 'nullable|string',
];
}我还有一个动态操作类,它负责整个应用程序中的文件上传:
class UploadImageAction implements UploadImageContract
{
public function handle(Request $request, $imageProperty, $image, $imageDir)
{
if ($request->hasFile($imageProperty)) {
// Handle uploading lf_image
if (!is_null($image) && Storage::exists($image)) {
// Throw exceptions here
Storage::delete($image);
}
// Throw exceptions here
return $request->file($imageProperty)->store($imageDir);
}
}
}我resolve()服务类中的操作,如下所示:
public function handleAttachments($request, $report)
{
// Handle Attachments
$uploadImageAction = resolve(UploadImageAction::class);
// Handle attachment action
if($request->file('attachment')) {
$report->attachment = $uploadImageAction->handle($request, 'attachment', $report->attachment, 'reports');
}
return $report;
}然后在handleAttachments()中使用storeReport()方法
public function storeReport(Request $request)
{
$report = new Report();
$report->fill($request->validated());
$report = $this->handleAttachments($request, $report);
$report->save();
if(!$report) {
throw new ReportException('Something went wrong with the request.' . $report);
}
return $report;
}因此,基本上,我将文件和数据合并到一个validated()请求中。这个实践在所有其他页面上都能很好地工作,但是由于我添加了Dropzone,我得到了上面在调试器中看到的错误。
当您提交表单时会发生什么?我试着让submit按钮处理队列并提交表单,但是随后我得到了验证错误,表示attachment field is required。
这是怎么回事?
非常感谢您的支持。
发布于 2022-10-12 10:58:50
尝试将“有时候”添加到文件字段的规则中。例如:
'ir_number' => 'sometimes|required|integer',
'type' => 'sometimes|required|integer',
'location' => 'sometimes|required|integer',
//.....
'attachment' => 'sometimes|nullable|image|mimes:jpeg,jpg,png,gif,webp',https://stackoverflow.com/questions/74038384
复制相似问题