Skip to content

Conversation

dummdidumm
Copy link
Member

@dummdidumm dummdidumm commented Oct 6, 2025

With type-safe forms via myForm.fields the myForm.buttonProps API is an outlier. It allows you to apply a completely different form action onto an existing form. That means you lose all type safety you've gained. We therefore remove it in favor of as('submit', 'value') - instead of buttonProps you declare a new field on your schema on which you can decide which logic to execute:

// +page.svelte
<script>
-  import { login, register } from './login.remote';
+  import { login} from './login.remote';
</script>

<form {...login}>
  <!-- ... -->
  <button>login</button>
-  <button {...register.buttonProps}>register</button>
+  <button {...login.fields.action.as('submit', 'register')}>register</button>
</form>

// login.remote.ts
import { form } from '$app/server';

export const login = form(
-  v.object({ username: v.string(), _password: v.string() })
+  v.object({ username: v.string(), _password: v.string(), action: v.optional(v.picklist(['login', 'register'])) })
  (payload) => {
+    if (payload.action === 'register') {
+      // register...
+    } else {
      // login...
+    }
  }
);

-export const register = form(
-  v.object({ username: v.string(), _password: v.string() })
-  (payload) => {
-    // register...
-  }
-);

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

use e.g. `<button {...myForm.fields.action.as('submit', 'register')}>Register</button>` button instead
Copy link

changeset-bot bot commented Oct 6, 2025

🦋 Changeset detected

Latest commit: f0b05c6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@svelte-docs-bot
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants