CaptainCodeman / svelte-form-helper

Lightweight helpers for form validation with Svelte
https://captaincodeman.github.io/svelte-form-helper/
MIT License
55 stars 0 forks source link

Feat: set form as untouched #12

Open gbouteiller opened 1 year ago

gbouteiller commented 1 year ago

Hello again :), Would it be possible to add a method to the form store to make all the fields untouched. The use case behind it is to reset the form after a successful submit. For now, this is a way of doing it :

<script lang="ts">
const fields: HTMLElement[] = [];
const initialData = {forename: '', surname: ''};
let formData = initialData;

const onSubmit = () => {
  /*... */
  formData = initialData;
  fields.forEach((field) => delete field.dataset.touched);
};
</script>

<form use:form on:submit|preventDefault={onSubmit}>
  <input type="text" use:forename bind:this={fields[0]} value={formData.forename}  />
  <input type="text" use:surname bind:this={fields[1]} value={formData.surname}  />
</form>