A modern, type-safe form management library for Svelte 5+.
npm install formulatte
# or
pnpm add formulatte
# or
bun add formulatte
<script lang="ts">
import { Form } from 'formulatte';
import * as yup from 'yup';
const schema = yup.object({
email: yup.string().email().required(),
password: yup.string().min(6).required()
});
const initialValues = {
email: '',
password: ''
};
async function handleSubmit(values) {
console.log('Form submitted:', values);
}
</script>
<Form {initialValues} validationSchema={schema} onSubmit={handleSubmit}>
{#snippet default(form)}
<div>
<input
bind:value={form.state.values.email}
onblur={() => form.validate('email')}
/>
{#if form.state.errors.email}
<span class="error">{form.state.errors.email}</span>
{/if}
</div>
<button type="submit" disabled={!form.state.isValid}>
Submit
</button>
{/snippet}
</Form>
The main component that manages form state and validation.
interface FormProps<T> {
initialValues: T;
validationSchema?: Schema;
onSubmit: (values: T) => Promise<void> | void;
validateOnBlur?: boolean;
validateOnChange?: boolean;
}
Available through the form snippet parameter:
interface FormState<T> {
values: T;
errors: Partial<Record<keyof T, string>>;
touched: Partial<Record<keyof T, boolean>>;
isSubmitting: boolean;
isValid: boolean;
isDirty: boolean;
}
validate(field?: string)
: Validate entire form or specific fieldsubmit()
: Submit the formreset()
: Reset form to initial valuessetFieldValue(field, value)
: Set field valuesetFieldTouched(field, touched)
: Mark field as touched<Form {initialValues} validationSchema={schema}>
{#snippet default(form)}
<input
bind:value={form.state.values.email}
onblur={() => form.validate('email')}
class:error={form.state.errors.email && form.state.touched.email}
/>
{/snippet}
</Form>
<Form
initialValues={initialValues}
onSubmit={async (values) => {
const isValid = await customValidation(values);
if (isValid) {
// proceed
}
}}
>
{#snippet default(form)}
<!-- form fields -->
{/snippet}
</Form>
We welcome contributions! Please see our contributing guidelines for details.
git checkout -b feature/amazing
)git commit -am 'Add amazing feature'
)git push origin feature/amazing
)# Install dependencies
npm install
# Run development server
npm run dev
# Run tests
npm test
# Build library
npm run package
MIT © [Nathanael Bonfim]
See LICENSE for more information.
sv
Made with lots of ☕