I've created a pretty basic form which contains two fields of array of file type.
<form id="form" method="post" action="{{ url("check") }}" enctype="multipart/form-data" files="true">
{{ csrf_field() }}
<input type="file" name="file[]">
<input type="file" name="file[]">
<input id="submit" type="submit">
</form>
I've added the enctype attribute for submission, however Laravel's validation system seems to not work properly. Indeed because errors are displayed for one field only :
{
"name": [
"file"
],
"error": [
"The file field is required."
]
}
I was expecting something like that (it displays when I remove the enctype attribute)
{
"name": [
"file.0",
"file.1"
],
"error": [
"The file.0 must be a file of type: pdf, docx, odt.",
"The file.1 must be a file of type: pdf, docx, odt."
]
}
For better understanding, here is my controller code :
public function validation(Request $request)
{
$rules = [
"file" => "required",
"file.*" => "mimes:pdf,docx,odt"
];
return Validator::make($request->all(),$rules);
}
public function check(Request $request)
{
return response()->json(["name"=>$this->validation($request)->errors()->keys(),"error"=>$this->validation($request)->errors()->all()]);
}
I don't understand that issue, did I make a mistake or is that a bug from Laravel ? I'm using the 5.6 version by the way.
UPDATE
I've tried to implement my own rules, I've used a loop since fields are the same array :
$rules = [];
foreach($request->input('file') as $key => $value) {
$rules["file.{$key}"] = 'required|mimes:pdf,docx,odt';
}
return Validator::make($request->all(),$rules);
But again it doesn't work WITH the enctype attribute.
Thank you a lot for your help.