KHAN's avatar
Level 4

Required Validation On Multiple File Upload Input Issue

Hi,

I am adding mime types to my multiple file upload input, but its making the field required when i have not set this option. I dont want this field to be required. Also i am not able to upload xls file even tho ive specified it below.

'attachments.*'     => 'mimes:pdf,xls,doc,docx,pptx,pps,jpeg,bmp,png|max:20000',
<input type="file" name="attachments[]" multiple>
0 likes
9 replies
slashequip's avatar

Try:

'attachments.*'     => 'sometimes|mimes:pdf,xls,doc,docx,pptx,pps,jpeg,bmp,png|max:20000',

I imagine that XLS isn't the actual mime type. To test upload an XLS file without validation and dump the request, Laravel should tell you what the mime type is then.

usama.ashraf's avatar

Try dumping the request and see whether the images arrive as an array or a nested array. Show the output here.

KHAN's avatar
Level 4

@tomi

Someone mentioned it in that thread you linked to

that didnt work, its as in the docs, simply image.* will validate each item in the array but the error will always come back as required
KHAN's avatar
Level 4

@wearewoolf

Removed validation and ran

$request->all()

The array object shows up as empty no matter whats in there. But ive checked the excel file and its deffi saved as .xls

array:1 [▼
  0 => UploadedFile {#676 ▼
    -test: false
    -originalName: "capbudg.xls"
    -mimeType: "application/vnd.ms-excel"
    -size: 30720
    -error: 0
    path: "/tmp"
    filename: "phpqD4uNf"
    basename: "phpqD4uNf"
    pathname: "/tmp/phpqD4uNf"
    extension: ""
    realPath: "/tmp/phpqD4uNf"
    aTime: 2016-08-25 14:17:40
    mTime: 2016-08-25 14:17:40
    cTime: 2016-08-25 14:17:40
    inode: 1968461
    size: 30720
    perms: 0100600
    owner: 1000
    group: 1000
    type: "file"
    writable: true
    readable: true
    executable: false
    file: true
    dir: false
    link: false
  }
]

@usama.ashraf

attachments: [
{ },
{ },
{ },
{ }
]
usama.ashraf's avatar

As far as not wanting it to be required is concerned, try this:

'attachments'        => 'array',
'attachments.*'     => 'file|mimes:pdf,xls,doc,docx,pptx,pps,jpeg,bmp,png|max:20000'

And make sure your form has this: enctype="multipart/form-data"

KHAN's avatar
Level 4

@usama.ashraf

Im getting the error

The attachments.0 must be a file.
        @if($errors->any())
                    @foreach($errors->getMessages() as $this_error)
                        <p style="color: red;">{{$this_error[0]}}</p>
                    @endforeach
                @endif

really im thinking of having a single file upload, but being able to add more dynamically using javascript and then i wonder if i can validate the dynamic single file uploads.

slashequip's avatar

@KHAN in your dump it says the mime type is -mimeType: "application/vnd.ms-excel" which does fit in with what Laravel is looking for see:

/vendor/symfony/http-foundation/File/MimeType/MimeTypeExtensionGuesser.php line329

Does your form have the multipart enctype attribute or have you sorted this now?

ssjc02's avatar

Nullable should be added like this: 'attachments.*' => 'nullable|mimes:pdf,xls,doc,docx,pptx,pps,jpeg,bmp,png|max:20000',

Please or to participate in this conversation.