Gathering detailed insights and metrics for svelte-formek
Gathering detailed insights and metrics for svelte-formek
npm install svelte-formek
Typescript
Module System
Node Version
NPM Version
Total Downloads
354
Last Day
1
Last Week
3
Last Month
5
Last Year
41
Minified
Minified + Gzipped
Latest Version
1.0.0
Package Id
svelte-formek@1.0.0
Unpacked Size
83.00 kB
Size
20.55 kB
File Count
11
NPM Version
6.13.1
Node Version
13.2.0
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
200%
3
Compared to previous week
Last month
66.7%
5
Compared to previous month
Last year
-12.8%
41
Compared to previous year
Svelte forms lib is a lightweight library for managing forms in Svelte, with an Formik like API.
Go to the newly released documentation website to understand the API and see examples.
This module is distributed via npm which is bundled with node and
should be installed as one of your project's dependencies
:
1npm install svelte-forms-lib
This package also depends on
svelte
. Please make sure you have it installed as well.
1<script> 2 import createForm from "svelte-forms-lib"; 3 4 const { form, handleChange, handleSubmit } = createForm({ 5 initialValues: { 6 name: "", 7 email: "" 8 }, 9 onSubmit: values => { 10 // make form submission request with `values` 11 } 12 }) 13</script> 14 15<form on:submit={handleSubmit}> 16 <label for="name">Name</label> 17 <input 18 type="text" 19 name="name" 20 bind:value={$form.name} 21 on:change={handleChange} 22 /> 23 24 <label>Email</label> 25 <input 26 type="email" 27 name="email" 28 bind:value={$form.email} 29 on:change={handleChange} 30 /> 31 32 <button type="submit">Submit</button> 33</form>
The createForm
function requires at minimum a initialValues
object which contains the initial state of the form and a submit
function which will be called upon submitting the form.
Because the library is built using the Store API in Svelte, the values exposed by createForm
are observables.
1// all observables returned by `createForm` 2const { form, errors, touched, isValid, isSubmitting, isValidating, state } = createForm({...})
Within the template they can be read using the $
prefix i.e. $form
, $errors
. For example to access isValid
we'll use the $
prefix in the template
1<p>This form is {$isValid} </p>
Another example regarding form or errors:
1<script> 2 const { form, errors, ...other } = createForm({...other}); 3</script> 4 5<input name="name" bind:value={$form.name} ...other /> 6{#if $errors.name} 7 <span>{$errors.name}</span> 8{/if}
The code example below is abbreviated for focus,
...other
represents the remaining handlers, config and props needed to run the code.
So make sure to use the $
prefix in the template for observable values returned by createForm
.
This library works best with yup for form validation.
1<script> 2 import createForm from "svelte-forms-lib"; 3 import yup from "yup"; 4 5 const { form, errors, handleChange, handleSubmit } = createForm({ 6 initialValues: { 7 name: "", 8 email: "" 9 }, 10 validationSchema: yup.object().shape({ 11 name: yup.string().required(), 12 email: yup.string().email().required() 13 }), 14 onSubmit: values => { 15 // make form submission request with `values` 16 } 17 }) 18</script> 19 20<form on:submit={handleSubmit}> 21 <label for="name">Name</label> 22 <input 23 type="text" 24 name="name" 25 bind:value={$form.name} 26 on:change={handleChange} 27 /> 28 {#if $errors.name} 29 <em>{$errors.name}</em> 30 {/if} 31 32 <label for="email">Email</label> 33 <input 34 type="email" 35 name="email" 36 bind:value={$form.email} 37 on:change={handleChange} 38 /> 39 {#if $errors.email} 40 <em>{$errors.email}</em> 41 {/if} 42 43 <button type="submit">Submit</button> 44</form>
Custom validation is also possible:
1<script> 2 import createForm from "svelte-forms-lib"; 3 import yup from "yup"; 4 5 const { form, errors, handleChange, handleSubmit } = createForm({ 6 initialValues: { 7 name: "", 8 email: "" 9 }, 10 validate: values => { 11 let error = {}; 12 if (values.name === '') { 13 error.name = "Name is required" 14 } 15 if (values.email === '') { 16 error.email = "Email is required" 17 } 18 return error; 19 }, 20 onSubmit: values => { 21 // make form submission request with `values` 22 } 23 }) 24</script> 25 26<form on:submit={handleSubmit}> 27 <label for="name">Name</label> 28 <input 29 type="text" 30 name="name" 31 bind:value={$form.name} 32 on:change={handleChange} 33 /> 34 {#if $errors.name} 35 <em>{$errors.name}</em> 36 {/if} 37 38 <label for="email">Email</label> 39 <input 40 type="email" 41 name="email" 42 bind:value={$form.email} 43 on:change={handleChange} 44 /> 45 {#if $errors.email} 46 <em>{$errors.email}</em> 47 {/if} 48 49 <button type="submit">Submit</button> 50</form>
Currently custom validation is only run when submitting the form. Field validation will be added in the near future.
Form
, Field
and ErrorMessage
To reduce the boilerplate it is also possible to use additional helper components i.e. Form
, Field
and ErrorMessage
. Usage can be done as follows:
1<script> 2 import { Form, Field, ErrorMessage } from "svelte-forms-lib"; 3 import yup from "yup"; 4</script> 5 6<Form 7 initialValues={{ 8 name: "", 9 email: "" 10 }} 11 validationSchema={yup.object().shape({ 12 name: yup.string().required(), 13 email: yup.string().email().required() 14 })} 15 onSubmit={values => { 16 alert(JSON.stringify(values, null, 2)) 17 }} 18> 19 <label>name</label> 20 <Field name="name" /> 21 <ErrorMessage name="name" /> 22 23 <label>email</label> 24 <Field name="email" /> 25 <ErrorMessage name="email" /> 26 27 <button type="submit">submit</button> 28</Form>
The components are using context API to get the form state and handlers so that you don't have to set that up. All props passed to the helper components will be passed down to the element it's mapped to. The Form
is mapped to <form>
, Field
to <input>
and ErrorMessage
to <small>
.
Svelte forms lib also support form arrays and nested fields. The name attribute in the inputs accept path like strings i.e. users[1].name
which allow us to bind to nested properties if the form requires it. See example below. Validation still works as expected.
1<script> 2 import createForm from "svelte-forms-lib"; 3 import yup from "yup"; 4 5 const { form, errors, state, handleChange, handleSubmit, handleReset } = createForm({ 6 initialValues: { 7 users: [ 8 { 9 name: "", 10 email: "" 11 } 12 ] 13 }, 14 validationSchema: yup.object().shape({ 15 users: yup.array().of( 16 yup.object().shape({ 17 name: yup.string().required(), 18 email: yup 19 .string() 20 .email() 21 .required() 22 }) 23 ) 24 }), 25 onSubmit: values => { 26 // make form submission request with `values` 27 } 28 }); 29 30 const add = () => { 31 $form.users = $form.users.concat({ name: "", email: "" }); 32 $errors.users = $errors.users.concat({ name: "", email: "" }); 33 }; 34 35 const remove = i => () => { 36 $form.users = $form.users.filter((u, j) => j !== i); 37 $errors.users = $errors.users.filter((u, j) => j !== i); 38 }; 39</script> 40 41<form> 42 {#each $form.users as user, j} 43 <label>name</label> 44 <input 45 name={`users[${j}].name`} 46 bind:value={$form.users[j].name} 47 on:change={handleChange} /> 48 {#if $errors.users[j].name} 49 <hint>{$errors.users[j].name}</hint> 50 {/if} 51 52 <label>email</label> 53 <input 54 name={`users[${j}].email`} 55 bind:value={$form.users[j].email} 56 on:change={handleChange} /> 57 {#if $errors.users[j].email} 58 <hint>{$errors.users[j].email}</hint> 59 {/if} 60 61 <button on:click={add}>+</button> 62 <button on:click={remove(j)}>-</button> 63 {/each} 64 65 <button on:click={handleSubmit}>submit</button> 66 <button on:click={handleReset}>reset</button> 67</form>
updateField
- hook to update form fieldFor imperative 3rd party libraries createForm
also return an updateField
function. The function accepts a name
and value
in order to update a field of the form. It serves as an escape hatch.
1<button on:change={() => updateField('receiveNewsletter', true)}> 2 receive newsletter 3</button>
which results in the form:
1{ 2 receiveNewsletter: true 3}
Please feel free to submit any issue as means of feedback or create a PR for bug fixes / wanted features.
No vulnerabilities found.
No security vulnerabilities found.