A simple yet powerful, lightweight form handling library for Svelte 5, designed with flexibility in mind giving you full control without being opinionated. It integrates seamlessly with Zod for validation. With built-in TypeScript autocompletion, error prevention, and a straightforward API, you can create forms that are both robust and easy to manage.
isValid
, isSubmitting
, and isDirty
states.npm install svelte-simple-form zod
<script lang="ts">
import useForm from 'svelte-simple-form';
import { z } from 'zod';
const schema = z.object({
name: z.string().min(1, 'Name is required'),
age: z.number().min(18, 'You must be at least 18'),
example: z.string().min(1, 'Name is required')
});
const { form, enhance } = useForm({
initialValue: { name: '', age: 0, example: '' },
schema,
onSubmit: async (data) => {
await new Promise((r) => setTimeout(r, 500)); // simulate fetch server
console.log(data);
form.setInitialValue(data); // refresh form, isDirty will be false again
}
});
</script>
<form use:enhance>
<input type="text" bind:value={form.data.name} />
<input type="number" bind:value={form.data.age} />
<input type="text" oninput={(e) => form.setField('example', e.target.value)} />
<p>Error name: {form.errors.name.join(', ')}</p>
<button type="button" onclick={() => form.reset()}>Reset</button>
<button>Submit</button>
</form>
form
: Represents the form state, including values, errors, and form-specific status (e.g., isValid, isSubmitting).enhance
: A function or utility for use form functionality.initialValue
: Initial form valuesonSubmit
: Callback for form submissiononChange
: Callback for form state changeschema
: Zod validation schemaform.{state}
data
: Form data valueinitialValue
: Form initial valueerrors
: Error messages, organized as an object { fieldName: string[] }
isValid
: Boolean indicating if the form is validisSubmitting
: Boolean indicating if the form is being submittedisDirty
: Boolean indicating if any field has been modifiedtouched
: Touched field, organized as an object { fieldName: boolean }
form.{method()}
setInitialValue(value)
: Set the initial value of a formsetData(field or object, fieldValue?)
: Set data for a specific field or multiple fields at oncesetField(field, value)
: Set the value of a form field dynamicallysetError(field, value)
: Set an error message for a specific fieldsetErrors(value)
: Set multiple error messages at once by passing an objectsetIsDirty(value?)
: Set form isDirty, with optional valuesetTouched(field or object, fieldValue?)
: Mark a specific field(with optional value) or set multiple fields as touchedsetIsSubmitting(value?)
Set form isSubmitting, with optinal valuereset()
: Reset all form fields to their initial valuesresetField(field)
: Reset a specific form fieldarrayField(field)
: Manage array fields {add, remove, have}
add(value)
: Add an item to the array fieldremove(value)
: Remove an item from the array fieldhave(value)
: Check if a value exists in the arraysubmit()
: Manual trigger the form submissionvalidate()
: Validate the form all fieldsvalidate(field or fields)
: Validate the form specific field or fields arraycapture()
: Retrieve all form data in its current state, commonly stored in svelte store
or $state
populate(value)
: Populate the form with previously saved data form.capture()
MIT