Redirect back with errors from file validation in AdonisJS 6

53 views Asked by At

I'm trying to make a file upload form with AdonisJS 6. I'm using EdgeJS as a template engine.

I have some validation checking the file that was sent, I can get those errors easily by accessing file.errors.

But I'm struggling to send those errors back to the view, I can't find the right way to do this.

Here my store function in my controller.

async store({ request, response }: HttpContext) {
    const data = request.all()
    const payload = await storeBookValidation.validate(data)

    const cover = request.file('cover', {
      extnames: ['png', 'jpg'],
    })

    if (!cover.isValid) {
      // IDK what to put here !!!
    }
...
}

I tried to return a response.redirect().back() but I can't add any body in it. Also checked the doc for file error handling but only found things for API return, it does not work for my case.

1

There are 1 answers

0
leodlplq On

Ok, I managed to handle the validation by using a validation as I was doing for my other inputs.

Controller : const { cover } = await request.validateUsing(storeBookCoverValidation)

If the validation fails, it redirects with the errors.

If anyone knows how to handle the error redirection by any other way, I still would love to know !

Thanks :)