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

Nested forms update the parent data store #296

Open
t348575 opened this issue Jun 9, 2024 · 1 comment
Open

Nested forms update the parent data store #296

t348575 opened this issue Jun 9, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@t348575
Copy link

t348575 commented Jun 9, 2024

Describe the bug

I have a strange use case where some nested components also have their own <form> with their own validation & submission, and noticed that the child components appear to also update the data store of the parent component.

Which package/s are you using?

felte (Svelte), @felte/validator-zod

Environment

To reproduce

App.svelte

<script lang="ts">
  import { createForm } from 'felte';
  import * as zod from 'zod';
  import { validator } from "@felte/validator-zod";
  import Child from './Child.svelte'

  type Data = {
    email: string;
    password: string;
  }

  let submitted: Data | undefined;

  const schema = zod.object({
    a: zod.string()
  })
  const { form, data } = createForm<Data>({
    onSubmit(values) {
      submitted = values;
    },
    extend: validator({schema}),
  });

  $: console.log($data)

</script>

<main>
  <h1>Basic Example - Svelte</h1>
  <form use:form>
    <fieldset>
      <legend>Sign In</legend>
      <label for="a">Email:</label>
      <input type="text" name="a" id="a" />
    </fieldset>
    <button type="submit">Submit</button>
    <button type="reset">Reset</button>
    <Child/>
  </form>
  {#if submitted}
    <section>
      <h2>Submitted values</h2>
      <pre>{JSON.stringify(submitted, null, 2)}</pre>
    </section>
  {/if}
</main>

Child.svelte

<script lang="ts">
  import { createForm } from 'felte';
  import * as zod from 'zod';
  import { validator } from "@felte/validator-zod";

  type Data = {
    email: string;
    password: string;
  }

  let submitted: Data | undefined;

  const schema = zod.object({
    child_a: zod.string(),
  })
  const child = createForm<Data>({
    onSubmit(values) {
      submitted = values;
    },
    extend: validator({schema}),
  });

</script>
<main>
  <h2>Child component:</h2>
  <form use:child.form>
    <fieldset>
      <legend>Sign In</legend>
      <label for="child_a">Email:</label>
      <input type="text" name="child_a" id="child_a" />
    </fieldset>
  </form>
</main>

Small reproduction example

https://stackblitz.com/edit/vitejs-vite-kfgcfh?file=src%2FApp.svelte

Screenshots

No response

Additional context

No response

@t348575 t348575 added the bug Something isn't working label Jun 9, 2024
@Jylth
Copy link

Jylth commented Jul 11, 2024

Same issue on Solid.js.

This is a very annoying issue when building more complex views which are highly composable. It reduces the dev UX a lot and forces you to be careful or to find other ways to compose views through Portals which feels very hacky.

Thank you for your support and hard work.

I can help if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants