Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation not triggered when Inertia transform changes keys. #84

Open
ragulka opened this issue Jul 29, 2024 · 3 comments
Open

Validation not triggered when Inertia transform changes keys. #84

ragulka opened this issue Jul 29, 2024 · 3 comments
Assignees
Labels

Comments

@ragulka
Copy link

ragulka commented Jul 29, 2024

Laravel Precognition Plugin Version

0.5.6

Laravel Version

10.41.1

Plugin

Vue w/ Inertia

Description

In #72, support for Inertia transform was added. However, it looks like it does not support renaming keys, or changing the data structure. In fact, it looks like it only supports transforming individual form field values.

If keys are renamed, or the data structure changes when transforming, then validation is not triggered.

I would expect that changing the data structure with transform is possible and fully supported.

Steps To Reproduce

Implement a form like this:

const form = useForm('patch', '/update', {
    document: {
        customer: { email: '[email protected]' }
        /** other data here **/
    }
}).transform((data) => { ...data.document })
<input type="text" v-model="form.document.customer.email" @change="form.validate('customer.email')" />

Changing the email value will trigger validation all the way until this line:

return originalInputs.reduce<Partial<Data>>((carry, name) => ({
...carry,
[name]: data[name],
}), {}) as Data

It stops here, because the keys in originalInputs are based on the initial data, without applying the transformer on it.

@ragulka
Copy link
Author

ragulka commented Jul 29, 2024

I think the issue might be that the originalnputs array serves 2 different purposes:

  1. A stored list of original data keys, so it's easier to reset the form data
  2. A list of keys which is used to remove the non-data form properties from the form when calling form.data()

The issue here is that originalInputs is never updated, not even when calling form.setData() with a completely different object than when initializing. However, since originalInputs is used to reset form data to initial state, this seems an expected behavior in that regard...

Perhaps it would make more sense to have a dedicated dataKeys array, which is updated whenever setData is called?

Also, I haven't tested yet wether precognition forms support Inertia's form.defaults()?

@timacdonald
Copy link
Member

@ragulka, thanks for raising this. I had not considered keys changing when I implemented the transform feature.

I believe I have a solution to this that I am working on and should have ready shortly.

@timacdonald
Copy link
Member

Opened a pull request with a failing test for this one: #92

Unfortunately, it is a tricky one to solve with how Precognition needs to work. Will keep hacking on it and see what we can come up with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants