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

Proxy object in nested array #425

Closed
nula1337 opened this issue May 7, 2024 · 6 comments
Closed

Proxy object in nested array #425

nula1337 opened this issue May 7, 2024 · 6 comments

Comments

@nula1337
Copy link

nula1337 commented May 7, 2024

Description
Hello, I have a form with <input type="date">, I'm using a dateProxy to convert it to the right type, that's working great. The issue I'm having is I have this zod object:

workDone: z
        .object({
            type: z
                .string()
                .nullish(),
            date: z
                .date()
                .nullish(),
            hour: z
                .number()
                .nullish(),
            worker: z
                .string()
                .nullish(),
        })
        .array()

This Zod object is an array, because I have an Add button for creating additional rows for user to submit.
The issue is I don't know how to reference inputs that are in the array in the proxy function.

Here is the loop I'm using to display the array inputs.

{#each $formData.workDone as _, i}
<Form.Field {form} name="workDone.date">
	<Form.Control let:attrs>
	       <Form.Label>Datum</Form.Label>
		<Input
			{...attrs}
			bind:value={$formData.workDone[i].date}
			type="date"
			class="focus-visible:ring-2 focus-visible:ring-offset-0 focus-visible:ring-primary/30 focus-visible:border-primary/70"
		/>
	</Form.Control>
	<Form.FieldErrors />
</Form.Field>
{/each}

I tried creating this dateProxy, but I'm not quite sure how to get it working. Would be glad for some help, thank you :)
const workDoneDateProxy = dateProxy(form, 'workDone.date', { format: 'date', empty: 'null' });

@nula1337 nula1337 added the bug Something isn't working label May 7, 2024
@ciscoheat
Copy link
Owner

Hello, since you're using Formsnap, I'm not exactly sure how to handle this. You should have dataType: 'json' enabled at least, since it's about nested data. If you can't figure it out, try asking in the Formsnap discord: https://discord.com/channels/1025195801095458887/1148992561130786888

@ciscoheat ciscoheat removed the bug Something isn't working label May 7, 2024
@nula1337
Copy link
Author

nula1337 commented May 9, 2024

Hello, thanks for the asnwer, I asked the same question in the formsnap discord, no answer yet. But I do have dataType: 'json' set.

I found that if I set the dateProxy manually like this: const workDoneDateProxy = dateProxy(form, 'workDone[0].date', { format: 'date', empty: 'null' }); it does work for the first item of the input array. So the question for me probably is how to set the dateProxy dynamically for the array items.

Any ideas how to do that? Thank you.

@ciscoheat
Copy link
Owner

You could make a reactive statement that creates the proxies, and use them in the each loop.

@nula1337
Copy link
Author

nula1337 commented May 9, 2024

Would you mind showing me a quick example of how to do that? Thank you so much ♥

@ciscoheat
Copy link
Owner

@nula1337
Copy link
Author

Thank you so much, works great, haven't thought about using the index as prop for a dateProxy component.

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

No branches or pull requests

2 participants