Create your form

Formisch consists of runes, components and methods. To create a form you use the createForm rune.

Form rune

The createForm rune initializes and returns the store of your form. The store contains the state of the form and can be used with other Formisch runes, components and methods to build your form.

<script lang="ts">
  import { createForm, Field, Form } from '@formisch/svelte';
  import * as v from 'valibot';

  const LoginSchema = v.object({
    email: v.pipe(v.string(), v.email()),
    password: v.pipe(v.string(), v.minLength(8)),
  });

  const loginForm = createForm({
    schema: LoginSchema,
  });
</script>

<Form of={loginForm}>
  <!-- Form fields will go here -->
</Form>

Configuration options

The createForm rune accepts a configuration object with the following options:

  • schema: Your Valibot schema that defines the form
  • initialInput: Initial values for your form fields (optional)
  • validate: When validation first occurs (optional, defaults to 'submit')
  • revalidate: When revalidation occurs after initial validation (optional, defaults to 'input')
const loginForm = createForm({
  schema: LoginSchema,
  initialInput: {
    email: 'user@example.com',
  },
  validate: 'initial',
  revalidate: 'input',
});

Multiple forms

When a page contains multiple forms, you can create separate form stores for each one:

<script lang="ts">
  import { createForm, Form } from '@formisch/svelte';
  import * as v from 'valibot';

  const LoginSchema = v.object({
    email: v.pipe(v.string(), v.email()),
    password: v.pipe(v.string(), v.minLength(8)),
  });

  const RegisterSchema = v.object({
    username: v.pipe(v.string(), v.minLength(3)),
    email: v.pipe(v.string(), v.email()),
    password: v.pipe(v.string(), v.minLength(8)),
  });

  const loginForm = createForm({ schema: LoginSchema });
  const registerForm = createForm({ schema: RegisterSchema });
</script>

<Form of={loginForm}><!-- … --></Form>
<Form of={registerForm}><!-- … --></Form>

Next steps

Now that you know how to create a form, continue to the add form fields guide to learn how to connect your input elements to the form using the Field component.

Contributors

Thanks to all the contributors who helped make this page better!

  • GitHub profile picture of @fabian-hiller

Partners

Thanks to our partners who support the project ideally and financially.

Sponsors

Thanks to our GitHub sponsors who support the project financially.

  • GitHub profile picture of @vasilii-kovalev
  • GitHub profile picture of @saturnonearth
  • GitHub profile picture of @ruiaraujo012
  • GitHub profile picture of @hyunbinseo
  • GitHub profile picture of @nickytonline
  • GitHub profile picture of @KubaJastrz
  • GitHub profile picture of @andrewmd5
  • GitHub profile picture of @Thanaen
  • GitHub profile picture of @caegdeveloper
  • GitHub profile picture of @bmoyroud
  • GitHub profile picture of @dslatkin