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
- feat: use merged request body during validation when mergeMultipartFieldsAndFiles flag is enabled Closes: #4777 — committed to Stars1233/Adonis by thetutlage 2 years ago
Verified with the same example and it works fine. You will need the following flag enabled inside the
adonisrc.tsfile.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
dataproperty for validation and deep mergerequest.allandrequest.files.For example, you can use the
deepmergepackage from npm and use it follows.Fixed in this release. https://github.com/adonisjs/bodyparser/releases/tag/v10.1.0 and https://github.com/adonisjs/core/releases/tag/v6.18.0
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 likevine.string(). There is no problem with other combination; it works perfectly fine. For example, this setup:functions correctly. However, when we attempt something like this:
we receive an error stating that the field must be defined.