A fast, light and batteries-included form library to make you more productive.
npm install svelte-headless-form
<script>
import { createForm } from 'svelte-headless-form';
const { submitForm, input, errors, values, register } = createForm({
validateMode: 'onBlur', // Defaults - Schemaless:onChange Schema:onBlur
initialValues: {
username: '',
password: ''
},
initialValidators: {
username: (value) => (value.length > 0 ? false : 'Name is required'),
password: (value) => (value.length > 0 ? false : 'Password is required')
}
});
</script>
<form on:submit|preventDefault={submitForm((values) => console.log(values))}>
<input
type="text"
bind:value={$values.username}
use:register={{ name: 'username' }}
/>
{#if $errors.username}
<div>
{$errors.username}
</div>
{/if}
<input
type="password"
bind:value={$values.password}
use:register={{ name: 'password' }}
/>
{#if $errors.password}
<div>
{$errors.password}
</div>
{/if}
<button type="submit">Submit</button>
</form>
Svelte Headless Form allows for 2 different validation implementations, called schema-based validation and schemaless validation. In the How To Use section we are demonstrating schemaless validation by giving each form value it's own validator in the initialValidators prop. If you are intereseted in using schema based validaton please give your createForm() a prop called 'validationResolver' which is a single function that returns an object with error strings located at the same path of the corresponding values. In the future we plan to have pre-built validation resolvers for all the major schema based validators like zod, yup and joi to name a few.
These roadmap features are not ordered by priority.
Please consider svelte-headless-form in beta until a 1.0 release.