laravel / precognition

Anticipate the outcome of a future HTTP request.
https://laravel.com/docs/precognition
MIT License
127 stars 31 forks source link

Transform data before validation #38

Closed elliotaldertonJump24 closed 1 year ago

elliotaldertonJump24 commented 1 year ago

On our form we use the inertia form transform() method to transform some fields before posting (i.e. a datepicker field which return an object into a relevant string). Is there anyway we can transform a field before validating it?

driesvints commented 1 year ago

Hi there,

Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:

However, this issue will not be locked and everyone is still free to discuss solutions to your problem!

Thanks.

TheSnip3r commented 1 year ago

I've a similar situation. I've a field that I used to do transformation to it before submission, but after using the package I cannot do that, since the reactivity and the submitted data for validation are coupled. The backend expects specific structure and the frontend expects a different one. I'm not sure how this can be resolved to be honest...

timacdonald commented 1 year ago

Hey folks, as a temporary solution, you could do the following:

import { client, useForm } from 'laravel-precognition-vue'

client.axios().interceptors.request.use((request) => {
    request.data = {
        ...request.data,
        emails: request.data?.emails?.join(',')
    }

    return request
})

const form = useForm(/* ... */);

form.transform(/* ... */)

I've added this as an improvement we could make to the package and have the validator support the transform method without the need for this workout.