form helpers for svelte
Not standing in the way and keeping everything smooth.
svelte-formup
is two-way bound by default. Change a value in your object, and it changes in your inputs.sveltes action feature is a unique way to attach custom logic to DOM elements. Combined with its great way of two-way binding and its reactive stores this allows to write very native looking forms with no boilerplate.
Adding yup allows to define a schema for value parsing and validation. Yup schema are extremely expressive and allow modeling complex, interdependent validations, or value transformations.
Styling forms in a consistent way has always been a problem. Everyone has her own opinion about it. svelte-formup
allows flexibel error reporting supporting both native and programmatic usage. Invalid form elements are marked supporting several validity state specific css selectors like :invalid. Additionally svelte-formup
adds CSS classes (is-error
, is-success
, is-dirty
, is-pristine
, is-validating
, is-submitting
and is-submitted
) for further custom styling. These classes maybe added to surrounding fieldsets or form group element depending on the validity state of the contained form elements. On non form elements these classes are set using the has-
prefix instead of is-
.
npm install svelte-formup
And then import it:
// using es modules
import { formup } from 'svelte-formup'
// common.js
const { formup } = require('svelte-formup')
Alternatively use UNPKG or jsDelivr packages.
Hotlinking from unpkg: (no build tool needed!)
import { formup } from 'https://unpkg.com/svelte-formup?module'
<script>
import * as yup from 'yup'
import { formup } from 'svelte-formup'
const { values, errors, dirty, validate, validity } = formup({
schema: yup.object().shape({
title: yup.string().oneOf(['Mr.', 'Mrs.', 'Mx.']).required(),
name: yup.string().required(),
email: yup.string().email().required(),
}),
onSubmit(data, context) {
console.log('onSubmit', { data, context })
},
})
</script>
<form use:validate>
<p use:validity>
<label for="title">title</label>
<select id="title" bind:value="{$values.title}">
<option></option>
<option>Mr.</option>
<option>Mrs.</option>
<option>Mx.</option>
</select>
</p>
<p use:validity>
<label for="name">name</label>
<input id="name" bind:value="{$values.name}" type="text" />
</p>
<p use:validity>
<label for="email">email</label>
<input id="email" bind:value="{$values.email}" type="email" />
</p>
<p>
<button type="submit">Submit</button>
<button type="reset">reset</button>
</p>
</form>
svelte-formup
does not provide any svelte components. Most projects have their own way of reporting errors. Below is an example component to simplify error handling.
<script>
import { getFormupContext } from 'svelte-formup'
export let at
const { invalid } = getFormupContext()
let error
$: error = $invalid.get(at)
</script>
{#if error}
<span class="error" aria-live="polite">
<slot {error}>{error.message}</slot>
</span>
{/if}
This could be used like (omitting identical code for brevity)
<script>
import IfError from './if-error.svelte'
</script>
<form use:validate>
<label for="name">name</label>
<input id="name" bind:value="{$values.name}" type="email" />
<IfError at="name" />
</form>
svelte-formup
exports two functions:
yup.string().email().required()
=> email required
form > :global(.valid.dirty)
This project is free and open-source, so if you think this project can help you or anyone else, you may star it on GitHub. Feel free to open an issue if you have any idea, question, or you've found a bug.
Thanks for being willing to contribute!
Working on your first Pull Request? You can learn how from this free series How to Contribute to an Open Source Project on GitHub
We are following the Conventional Commits convention.
npm start
: Starts a snowpack dev server using src/__preview__
npm test
: Run test suite including lintingnpm run build
: Generate bundlessvelte-formup
is open source software licensed as MIT.
Generated using TypeDoc