sveltejs / sveltejs/kit

Add a way to intercept and change form action result in handle hook

Open
#9,676 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the problem

There's no native way to intercept form actions and changing its result before being rendered.

Why I need this
I'm experimenting automatic endpoint/form validation based on the defined schema file. I will load all needed schemas in the hook handle and validate them and return result back to user. Something similar to what's in Fastify framework

For standalone endpoints this works fine. but with form actions, this is challenging as I have to render the page with the error props but sveltekit yet doesn't provide any mechanism to change that.

Describe the proposed solution

We could either have a seperate hook function in hooks.server or provide a callback in resolve function similar to transformPageChunk

Alternatives considered
  1. Manually define the desired behavior in each form action with a shared function. but this is redundant
  2. A hacky way to use transformPageChunk to replace form in kit.start(app, __sveltekit_dev.element, { node_ids: [0, 2], data, form: null, error: null }); with the desired results. but this is a hacky solution and requires more testing and could break pages and introduce security issues.
Importance

would make my life easier

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the hooks.server and resolve entry points, then compare the proposed callback with the existing transformPageChunk behavior and the form handling in kit.start. The work is complete when the project has a supported way to intercept and change form action results before they are rendered.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.