fabian-hiller / modular-forms

The modular and type-safe form library for SolidJS, Qwik and Preact
https://modularforms.dev
MIT License
1.02k stars 54 forks source link

[solidjs] How to use createResource or createAsync to load default values #230

Open vf-unordinary opened 2 months ago

vf-unordinary commented 2 months ago

Hi all!

as the title says, is it possible to "watch" createResource/createAsync to load initial values of the form in an async way?

Relying on createEffect to set them using setValues surely works, but for example, in a solid start application interferes with the form submission, firing before the actual form is submitted.

Thanks in advance!

fabian-hiller commented 2 months ago

I think in this case the best thing to do is to use createEffect with our reset method. https://modularforms.dev/solid/api/reset

vf-unordinary commented 2 months ago

Brilliant. It works.

Its a bit counterintuitive to use reset to load data, but nothing a good comment could make it future proof.

Thanks Fabian!

fabian-hiller commented 2 months ago

I know. 😐 It makes sense to "reset" the form with new initial values, but I understand that it feels wrong in the first place. Would you choose a different name or just improve the documentation?

vf-unordinary commented 2 months ago

In my mind, resetting just restore a previous state of the form. Data loading should define the initial state, until the next submit, which create a new state to rollback.

I've not digged through the code of your brilliant library, so I'm completely ignorant of the process behind: is it difficult to make the createForm's initiaValues signal aware? This would make compatible with both createAsync and createResource.

fabian-hiller commented 2 months ago

Thank you for your feedback and recommendation. I will think about it! I agree that it can improve the DX a lot in these cases.

vf-unordinary commented 2 months ago

Hey Fabian! I've found a case when the reset way of filing the form does not work: when a FieldArray is present. It shows the correct number of elements, but no values in them.

I've tried to JSON.stringify the field property returned by the Field component: this is the content passed with reset {"name":"variants.0.code","error":"","active":true,"touched":false,"dirty":false}

while this is what it pass when using setValues {"name":"variants.0.code","value":"CO400","error":"","active":true,"touched":true,"dirty":true}

fabian-hiller commented 2 months ago

I know, and I am sorry. This is a known bug. There should already be an open issue. I hope to find some time one day to fix such problems.