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

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

Open
vf-unordinary opened this issue Jul 22, 2024 · 7 comments
Assignees
Labels
enhancement New feature or request

Comments

@vf-unordinary
Copy link

vf-unordinary commented Jul 22, 2024

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
Copy link
Owner

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

@fabian-hiller fabian-hiller self-assigned this Jul 23, 2024
@fabian-hiller fabian-hiller added the question Further information is requested label Jul 23, 2024
@vf-unordinary
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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.

@fabian-hiller fabian-hiller added enhancement New feature or request and removed question Further information is requested labels Jul 23, 2024
@vf-unordinary
Copy link
Author

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
Copy link
Owner

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.

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

No branches or pull requests

2 participants