Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1879

Changed a form dataType to json and now need to add a new field on submit. How?

$
0
0

I have a form, that was working with dataType:'form', but needed to add a nested form inside the original one and so changed the dataType to 'json'. Here is the schema:

 z.object({    typeId: z.coerce.number().min(1),    locationId: z.coerce.number().min(1),    associatedInfo: infoFormSchema.optional(),    plus: z.string()});

The associatedInfo is filled with nested form.
I applied this change to multiple forms and all worked fine. But on this specific, the plus field is added onSubmit

const formOptions: FormOptions<Infer<PutFormSchema>> = {    resetForm: false,    invalidateAll: false,    onSubmit: ({ formData }) => {        formData.set('plus',            JSON.stringify({                slots: Object.values($store.selection),                count: $store.selection.count,                toSelectSlots: $info.toSelectCount            })        );    }    //more things}

These formOptions are passed to the form and used like this:

const form = superForm(data, {    validators: zodClient(formSchema($page.data.locale)),    applyAction: false,    dataType: 'json', //This was added to work with the nested form    ...formOptions,});

In the main form, i call the nested form using the same form, like this:

<FormNested    selectOptions={{ ... }}    form={form}></FormNested>

I think the problem is on the variable that im using on the onSubmit, because formData comes filled with both info from the main and nested form, but it didnt add the plus field info. The issue is that the plus filled is empty when arrives at page.server.


Viewing all articles
Browse latest Browse all 1879

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>