core: Array of object with file and a string field throwing "string field must be defined" message always.

Package version

Adonis Core Version: 6.14.1, vineJS version: 2.1.0

Describe the bug

Using Adonisjs with inertia + vue(SSR)

// router.ts
router.post('/upload', async ({ request, response }) => {
  const data = await request.validateUsing(
    vine.compile(
      vine.object({
        images: vine.array(vine.object({ file: vine.file({ size: '2mb' }), title: vine.string() })),
      })
    )
  )
  return response.json(data)
})

//home.vue
<script setup lang="ts">
import { Head, useForm } from '@inertiajs/vue3'

const getError = (error: any, key: string) => {
  return error?.[key]?.join(',')
}

const form = useForm<{
  images: { file: File | undefined; title: string }[]
}>({
  images: [{ file: undefined, title: '' }],
})
</script>

<template>
  <Head>
    <title>Home</title>
  </Head>
  <h1>Form</h1>
  <div>
    <form
      @submit.prevent="form.post('/upload')"
      style="display: flex; flex-direction: column; gap: 1rem; padding: 3rem"
    >
      <template v-for="(_, index) in form.images" :key="index">
        <input type="text" placeholder="Title" v-model="form.images[index].title" />
        <span>{{ getError(form.errors, `images.${index}.title`) }}</span>
        <input
          type="file"
          @input="form.images[index].file = ($event.target as HTMLInputElement)?.files?.[0]"
        />
        <span>{{ getError(form.errors, `images.${index}.file`) }}</span>
      </template>
      <button type="button" @click="form.images.push({ file: undefined, title: '' })">
        Add image
      </button>
      <button type="submit" :disabled="form.processing">Submit</button>
    </form>
  </div>
</template>
`

//Respone
"props": {
        "errors": {
            "images.0.title": [
                "The title field must be defined"
            ],
            "images.1.title": [
                "The title field must be defined"
            ],
            "images.2.title": [
                "The title field must be defined"
            ]
        }
    },

Reproduction repo

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 18 (6 by maintainers)

Commits related to this issue

Most upvoted comments

Verified with the same example and it works fine. You will need the following flag enabled inside the adonisrc.ts file.

import { defineConfig } from '@adonisjs/core/app'

export default defineConfig({
  experimental: {
    mergeMultipartFieldsAndFiles: true,
  },

  // ... rest of the config
})

I think this issue was reported earlier as-well (maybe in the Discord). The files and the request body are two different objects and hence when you expect them to be present together than you will have to self define the data property for validation and deep merge request.all and request.files.

For example, you can use the deepmerge package from npm and use it follows.

import merge from 'deepmerge'

request.validateUsing(validator, {
  data: merge(request.all(), request.allFiles())
})

Deep merging will be implemented, but since it will be a breaking change, this has to wait a little 😃

Hey @RomainLanz 👋

Please note that

The issue only occurs when we use vine.file() together with other types like vine.string(). There is no problem with other combination; it works perfectly fine. For example, this setup:

tags: vine.array(vine.object({ key: vine.string(), value: vine.string() }))

functions correctly. However, when we attempt something like this:

images: vine.array(vine.object({ file: vine.file({ size: '2mb' }), title: vine.string() }))

we receive an error stating that the field must be defined.