File upload inputs are handled a little differently in a Laravel request than other types of inputs. For example, a text input when empty will still be present in $request->input()
. On the other hand, an empty file input is not set in $request->input()
or $request->file()
.
Your sample rule, 'Equipment_Cert.*' => 'required'
means "for every field in the Equipment_Cert
array on this request, it should have a value". But because empty file inputs are stripped from the request, there is no Equipment_Cert
array, so there are no elements in that array for this rule to be applied to.
If you want to make sure that there is a file element uploaded for every row in your dynamic form, you could do something like this:
// 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',
],
]);
With this rule, you'll get an error like The equipment cert must contain 3 items
assuming there were 3 rows on the dynamic form. And if you wanted, you could further customize this message to something a little nicer, like Each equipment row requires an uploaded certificate.
Or, if you're able to change your HTML form structure, you could make each dynamic row in your form it's own array. Name your fields something like equipment[][name]
and equipment[][certificate]
and then your rules could be closer to what you originally tried:
[
'equipment.*.name' => 'required',
'equipment.*.certificate' => 'required',
]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…