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 dirty #11

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 set it as dirty (set all its fields as dirty) in order for example to display all invalid fields at the same time when clicking on submit? For now, this is a way of doing it :

<script lang="ts">
const fields: HTMLElement[] = [];

const invalidate = () => fields.forEach((field) => {
  field.focus();
  field.blur();
});

const onSubmit = () => {
  if (!$form.valid) return invalidate();
  /*... */
};
</script>

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