Skip to content
AKCodeWorksPublic

About

A superform style API for sveltekit remote forms

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Formy

I wanted Superforms-style hooks for SvelteKit remote forms. Use Formy anywhere you'd use a remote form. Just spread it onto <form> and keep using fields.name.as('text') on your inputs.

Install

Your app needs SvelteKit 3 with remote functions enabled.

npm install @akcodeworks/formy

Use it

Start with a SvelteKit remote form. The schema goes here.

// greet.remote.ts
import { form } from '$app/server';
import { z } from 'zod';

export const greet = form(
	z.object({ name: z.string().trim().min(1, 'Enter your name') }),
	({ name }) => {
		return { message: `Hi, ${name}` };
	}
);
<script lang="ts">
	import { Formy } from '@akcodeworks/formy';
	import { greet } from './greet.remote';

	let message = $state('');

	const form = new Formy({
		form: greet,
		onSubmit: () => {
			message = '';
		},
		onSuccess: ({ result }) => {
			message = result?.message ?? '';
		},
		onFail: ({ issues }) => {
			message = issues?.[0]?.message ?? 'Check the form and try again.';
		}
	});
</script>

<form {...form}>
	<label>
		Name
		<input {...form.fields.name.as('text')} />
	</label>
	<button type="submit" disabled={form.pending > 0}>Say hi</button>
	<p role="status">{message || form.firstIssue?.message || form.result?.message || ''}</p>
</form>

Hooks

Hook When to use it
onSubmit({ form }) Run code before sending the request. Return false to cancel.
onSuccess({ form, result }) Use the value returned by the remote form after a successful submission.
onFail({ form, issues }) Handle validation issues from the remote form, including issues returned with SvelteKit's invalid(...).
onError({ form, error }) Handle an error from the submission, onSubmit, or updates. A request or refresh can fail after a server write has already happened.
onHookError({ form, hook, error, outcome }) Handle an error thrown by onSuccess, onFail, or onFinally. You still get the original outcome.
onFinally({ form, status }) Run after Formy handles the attempt. The status is success, fail, error, or cancelled. Narrow it to get the result, issues, or error.

Each hook can be async. The form passed to a hook is SvelteKit's enhance callback instance, with access to the mounted form.element. Without an onError or onHookError handler, the corresponding error goes to SvelteKit.

The browser and SvelteKit's optional preflight validation can stop a submission before it reaches the hooks. With JavaScript off, the form still posts to the remote function.

Working with the form

The SvelteKit API is available directly on Formy. That includes fields, preflight(), validate(), submit().updates(), enhance(), and for(id) on forms with an id field.

  • form.fields gives you SvelteKit's typed fields. Use form.errors for all issues or form.firstIssue for the first one.
  • form.dirty tells you whether a field was edited since mount or reset. It stays true if you type the original value back in.
  • form.result, form.pending, form.submitted, and form.element expose SvelteKit's state.
  • Add form.preflight(schema) for browser-side validation. Keep that schema in a shared file if you also use it in your remote function. Call form.validate() to validate on demand.
  • Set resetOnSuccess: false to keep the inputs after a successful submission.
  • Use the updates option to return the queries or optimistic updates to apply with the submission.

Call form.submitEnhanced() to submit through the browser and run the hooks. You can pass a submit button to include its value and overrides. The call returns immediately. Use onFinally for the outcome.

Call form.submit() or form.submit().updates(...queries) to submit directly through SvelteKit without the hooks. Formy allows one active submission per remote form instance. A second enhanced attempt reports cancelled. A second direct call through Formy resolves to false.

form.enhance(callback) hands submission handling to your callback. Spread form directly to use the hooks from the constructor.

form.for(id) gives you the same keyed remote form you get from SvelteKit. Wrap that instance in new Formy({ form: form.for(id), ... }) if the row needs hooks too.

The original SvelteKit form is available as form.form. Submitting through it directly uses SvelteKit's own submission behavior.

Develop locally

Use Node 24. The documentation site is the root route, and the guide lives in src/routes/docs/guide.svx.

npm install
npm run dev
npm run check
npm test

Build locally

Command What it does
npm run build:web Builds the docs and playground into build/.
npm run run:web Starts the built docs server. Set HOST and PORT to change where it listens.
npm run build:app Builds and checks the npm package in dist/.

Deploy the docs as a Node server. Set ORIGIN to the public URL when deploying behind a proxy so the playground can submit to the same origin.

About

A superform style API for sveltekit remote forms

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages