Skip to content

fix: clear form issues when error() is used after validation passes#14666

Closed
Copilot wants to merge 3 commits intomainfrom
copilot/fix-issues-clearing-in-form
Closed

fix: clear form issues when error() is used after validation passes#14666
Copilot wants to merge 3 commits intomainfrom
copilot/fix-issues-clearing-in-form

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Oct 9, 2025

Problem

When a remote form is submitted with invalid values, validation issues are displayed. If the form is then resubmitted with valid values but the handler calls error() (e.g., for authentication), the validation issues remain visible even though validation passed.

<form {...createPost.enhance(async ({ submit }) => {
  try {
    await submit()
  } catch (error) {
    console.log('error occurred')
  }
})}>
  <input {...createPost.fields.title.as('text')}>
  {#if createPost.fields.title.issues()}
    <!-- These issues don't clear when validation passes but error() is thrown -->
    <p>{createPost.fields.title.issues()!.at(0)!.message}</p>
  {/if}
</form>
export const createPost = form(
  object({
    title: pipe(string(), nonEmpty())
  }),
  async (data) => {
    const currentUser = await getCurrentUser()
    if (!currentUser) {
      error(401) // Validation passed, but issues aren't cleared on client
    }
    // proceed
  }
)

Solution

The client had no way to know that validation passed when an error was thrown. The fix ensures validation state is communicated even when errors occur:

  1. Server: Set output.issues = {} when validation passes (before calling user handler)
  2. Server: Always cache the output so validation state is preserved even if an error is thrown
  3. Server: Include cached issues in error response when available
  4. Client: Parse issues from error response; if empty (validation passed), clear old issues before throwing the error

Changes

  • packages/kit/src/runtime/app/server/remote/form.js: Set and cache empty issues when validation passes
  • packages/kit/src/runtime/server/remote.js: Include issues from cache in form error responses
  • packages/kit/src/runtime/client/remote-functions/form.svelte.js: Clear issues when error response indicates validation passed
  • packages/kit/src/types/internal.d.ts: Add optional issues field to ServerErrorNode type
  • Added test case to verify issues are cleared when valid data triggers an error

Fixes #[issue_number]

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • esm.ubuntu.com
    • Triggering command: /usr/lib/apt/methods/https (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Valid field issues are not cleared when error is used in remote form handler</issue_title>
<issue_description>### Describe the problem

  1. Submit form with intentionally invalid values
  2. Invalid fields will have issues
  3. Submit form again but make sure it's valid
  4. If error is used inside form, fields issues are not updated even though validation passes

Enhanced form:

<form {...createPost.enhance(async ({ submit }) => {
  try {
    await submit()
  } catch (error) {
    console.log('oh wow, we have an error')
  }
})}>
  <input {...createPost.fields.title.as('text')}>
  {#if createPost.fields.title.issues()}
    <p>{createPost.fields.title.issues()!.at(0)!.message}</p>
  {/if}
</form>

Remote form:

export const createPost = form(
  object({
    title: pipe(string(), nonEmpty())
  }),
  async (data) => {
    const currentUser = await getCurrentUser()

    if (!currentUser) {
      return error(401)
    }

    // proceed
  }
)

Describe the proposed solution

Field issues should be updated even with error since validation passes and handler is executed

Alternatives considered

Current workaround is to manually validate in catch:

<form {...createPost.enhance(async ({ submit }) => {
  try {
    await submit()
  } catch (error) {
    await createPost.validate({ includeUntouched: true })
  }
})}>

Importance

would make my life easier

Additional Information

@sveltejs/kit - 2.46.1
svelte - 5.39.9
</issue_description>

<agent_instructions>look into form.js and form.svelte.js to fix the bug. add a test to the test/apps/basic app at src/routes/remote/form/validate by enhancing my_form in form.remote.ts to error(...) in case of a specific value set to then test that the issues are cleared.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

Fixes #14646


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Oct 9, 2025

⚠️ No Changeset found

Latest commit: ec0191f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@svelte-docs-bot
Copy link
Copy Markdown

Copilot AI and others added 2 commits October 9, 2025 11:52
Co-authored-by: dummdidumm <5968653+dummdidumm@users.noreply.github.com>
Co-authored-by: dummdidumm <5968653+dummdidumm@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix issues not clearing in remote form handler fix: clear form issues when error() is used after validation passes Oct 9, 2025
Copilot AI requested a review from dummdidumm October 9, 2025 12:00
@dummdidumm
Copy link
Copy Markdown
Member

useless

@dummdidumm dummdidumm closed this Oct 9, 2025
@dummdidumm dummdidumm deleted the copilot/fix-issues-clearing-in-form branch October 9, 2025 12:14
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.

Valid field issues are not cleared when error is used in remote form handler

2 participants