首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel 9:下拉区域显示[对象对象]和返回错误(字段是必需的)?

Laravel 9:下拉区域显示[对象对象]和返回错误(字段是必需的)?
EN

Stack Overflow用户
提问于 2022-10-12 08:00:08
回答 1查看 108关注 0票数 0

首先,我遵循了Dropzone:数据与文件相结合的文档

我创建了一个自定义按钮来处理排队的文件,而不是表单的默认提交按钮。

这是我的Dropzone JS代码块:

代码语言:javascript
复制
// 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:

代码语言:javascript
复制
<!--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无法读取名为“附件”的输入,因为它不存在并被Dropzone替换。

现在让我们来讨论这个问题。

每次我试图处理排队的文件时,都会在laravel调试器中得到以下错误:

ir number实际上是表单中的一个字段,并且是填充的!实际上,表单中所有必需的字段都已被填充。当然,因为它是laravel,所以我有一个请求文件,它告诉需要哪个字段以及它是什么类型的字段:

代码语言:javascript
复制
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',
        ];
    }

我还有一个动态操作类,它负责整个应用程序中的文件上传:

代码语言:javascript
复制
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()服务类中的操作,如下所示:

代码语言:javascript
复制
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()方法

代码语言:javascript
复制
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

这是怎么回事?

非常感谢您的支持。

EN

回答 1

Stack Overflow用户

发布于 2022-10-12 10:58:50

尝试将“有时候”添加到文件字段的规则中。例如:

代码语言:javascript
复制
'ir_number'              => 'sometimes|required|integer',
'type'                   => 'sometimes|required|integer',
'location'               => 'sometimes|required|integer',

//.....

'attachment'             => 'sometimes|nullable|image|mimes:jpeg,jpg,png,gif,webp',
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74038384

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档