简体   繁体   中英

laravel 8 validate array input with axios

and this is my request code ..

public function rules()
{
    return [
      'file' => 'nullable|array|min:1',
      'file.*' => 'nullable|file|mimes:xlsx|max:50000', 
    ];
}

and this is the html code

<form class='curd-form' id='form' @submit.prevent='curd_create("{{$model[0]}}")' method='post' action='{{$path}}{{$model[0]}}' enctype="multipart/form-data">
    <input id='file' class='btn btn-success' type='file' {{$file_requires ?? null}} accept='{{$file_types ?? null}}'  multiple  name='file[]' />
    <input type='submit' />                 
</form>

now if i remove the validation everything working good .. but if i try to upload excel file i got this error ...

在此处输入图片说明

and this is the excel file

New Microsoft Excel Worksheet.xlsx

now this happened with any type of file i add to the rules validation if i add

'file.*' => 'nullable|file|mimes:xlsx,jpg,png,pdf|max:50000', 

i gat the same error to the same extension .. and this is the axios code to submit the form

this.show_curd_errors = false;
this.errors = [];
var frm = $('.curd-form');
let formData = new FormData(frm[0]);
var add_or_print = $("#add_or_print").val();
axios.post(path+page_title,formData)
.then((response) => 
{
    console.log(response)
}).catch (error => {
    console.log(error);
    var error = error.response['data']['errors'];
    this.errors = error;
    hide_loading();
});

what is the error in my code .. thanks ..

You have Declare Mime Types As This:

https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types

this will work:

   'file.*' => 'required|file|max:600|mimetypes:application/vnd.ms-excel',

The mime rule, controls The file Extention.

your Excel file may be .xlsx or .xls

but mimetypes dosent care the file extension.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM