LARLAVEL中动态输入字段的验证 [英] Validation of the dynamic input field in laravel

查看:28
本文介绍了LARLAVEL中动态输入字段的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,可以在其中动态添加行,并且用户可以提交任何行的文件。我这里有一个问题,我如何验证文件输入?

我正在使用jQuery动态添加/删除行:

                            var row = 

                            "<tr> <input type='hidden' name='Registration_Tag[]'' value='" + Registration_Tag + "'>" +
                            "<td class='px-6 py-4 whitespace-nowrap'>" +
                            "<div class='flex items-center'>"+
                            "<div class='ml-4'>"+
                            "<div class='text-sm font-medium text-gray-900'>"+
                            Equipment_Name+
                            "</div>"+
                            "</div>"+
                            "</div>"+
                            "</td>"+
                            "<td class='px-6 py-4 whitespace-nowrap'>"+
                            "<div class='text-sm text-gray-500'>"+
                            Registration_Tag+
                            "</div>"+
                            "</td>"+
                            "<td class='px-6 py-4 whitespace-nowrap'>"+
                            "<span"+
                            "class='px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800'>"+
                            Equipment_Status+
                            "</span>"+
                            "</td>"+
                            "<td class='py-4 whitespace-nowrap text-sm text-gray-500'>"+

                            "<div class='flex flex-wrap my-auto mb-6'>"+
                            "<div class='w-full px-3'>"+
                            "<input id='grid-password' type='file' placeholder='' name='Equipment_Cert[]'>"+
                            " <small class='text-danger'>{{ $errors->first('Equipment_Cert') }}</small>"+
                            "</div>"+
                            "</div>"+
                            "</td>"+
                            "<td class='px-6 py-4 whitespace-nowrap text-right text-sm font-medium'>"+
                            "<button type='button' class='remove-tr close'>"+
                            "<span aria-hidden='true'>&times;</span>"+
                            "</button>"+
                            "</td>"+
                            "</tr>"

                            $("#Calibration_Table").append(row);

我已经尝试了下面这样的点表示法,但对我仍然没有用处。


                    $v = Validator::make($request->all(), [
                        'Calibration_Location'=>'required',
                            'Calibration_Category'=>'required',
                            'Date_of_Calibration' => 'required',
                            'Next_Due_Date' => 'required',
                            'Equipment_Cert.*' => 'required'
                        ]);
                    if ($v->fails()) {
                        return redirect('/Equipments/create?request_type=Update+Calibration+for+All+Category')
                                    ->withErrors($v->errors())
                                    ->withInput();
                    }

希望能从你那里得到一些建议。提前感谢

推荐答案

文件上载输入在Laravel请求中的处理方式与其他类型的输入略有不同。例如,文本输入为空时仍将显示在$request->input()中。另一方面,$request->input()$request->file()中没有设置空文件输入。

您的示例规则'Equipment_Cert.*' => 'required'表示对于此请求的Equipment_Cert数组中的每个字段,它都应该有一个值";。但是,由于从请求中剥离了空文件输入,因此没有Equipment_Cert数组,因此该数组中没有要应用此规则的元素。

如果要确保动态表单中的每一行都上载了一个file元素,可以执行以下操作:

// I picked this field to count because it's a text input in your dynamic row
$dynamicRowCount = is_array($this->input('Registration_Tag')) ? count($this->input('Registration_Tag')) : 0;

$v = Validator::make($request->all(), [
    'Calibration_Location'=>'required',
    'Calibration_Category'=>'required',
    'Date_of_Calibration' => 'required',
    'Next_Due_Date' => 'required',
    'Equipment_Cert' => [
        'required',
        'array',
        "size:$dynamicRowCount",
    ],
    
    // you can still do further validation on each file if necessary
    'Equipment_Cert.*' => [
        'file',
        'size:4096',
        'mimes:pdf,docx',
    ],
]);

使用此规则时,假设动态表单上有3行,您将得到类似The equipment cert must contain 3 items的错误。如果您愿意,还可以将此消息进一步自定义为更好的内容,如Each equipment row requires an uploaded certificate.

或者,如果您能够更改HTML表单结构,则可以使表单中的每个动态行都成为自己的数组。将字段命名为类似于equipment[][name]equipment[][certificate]的名称,这样您的规则可能更接近您最初尝试的规则:

[
    'equipment.*.name' => 'required',
    'equipment.*.certificate' => 'required',
]

这篇关于LARLAVEL中动态输入字段的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆