laravel/precognition

Transform data before validation

elliotaldertonJump24 opened this issue · 3 comments

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?

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.

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...

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.