Skip to content

fix: make form fields.value() reflect input_value passed to .as() - #15690

Closed
DrUse1 wants to merge 11 commits into
sveltejs:mainfrom
DrUse1:fix/remote-form-value-undefined-with-default
Closed

DrUse1 wants to merge 11 commits into
sveltejs:mainfrom
DrUse1:fix/remote-form-value-undefined-with-default

Conversation

@DrUse1

@DrUse1 DrUse1 commented Apr 9, 2026 •

Copy link
Copy Markdown

Fixes #15707 :

<form {...test_form}>
	// not displaying 'default text'
	<p>{test_form.fields.name.value()}</p>

	// displays 'default text'
	<input {...test_form.fields.name.as('text', 'default text')} />
</form>

When calling fields.name.as('text', 'default'), the input_value was used as a display fallback in the returned props but was never accessible to fields.name.value(), which returned undefined until the user interacted with the input.

When .as() is called with an input_value, it now writes it to the cache via set_input, so get_value() can immediately return it.


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

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:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

@changeset-bot

changeset-bot Bot commented Apr 9, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f28519d

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 Patch

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
Copy Markdown

@DrUse1 DrUse1 closed this Apr 10, 2026
@DrUse1 DrUse1 reopened this Apr 10, 2026
Comment thread packages/kit/src/runtime/form-utils.js Outdated
Comment on lines +718 to +720
if (input_value !== undefined) {
defaults[key] = input_value;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could lead to out of sync values if the value changes in an event, and then it's read synchronously the line after

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this would not work if the code in the repro changes to this

<form {...test_form}>
	// not displaying 'default text'
	<p>{test_form.fields.name.value()}</p>
	// displays 'default text'
	<input {...test_form.fields.name.as('text', 'default text')} />
</form>

@DrUse1 DrUse1 Apr 12, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried puting the display before the input and it still works

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will put it like this in the test to be sure

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this only works by chance and the fact that default is not stateful makes it even more weird. What should happen in this case for example?

<form {...test_form}>
	// not displaying 'default text'
	<p>{test_form.fields.name.value()}</p>
	// displays 'default text'
	{#if show}
		<input {...test_form.fields.name.as('text', 'default text')} />
	{/if}
</form>

when show become true? What about when it become false?

I'll check with the rest of the team but I think we should probably just document this behaviour considering how weird it is.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me, I’ll let you take it from here. I don't want to mess this up :)

DrUse1 added 5 commits April 12, 2026 19:03
placing value display before input
….com:DrUse1/kit into fix/remote-form-value-undefined-with-default
CAREFUL : I needed to change a pre-existing test that was failing
@elliott-with-the-longest-name-on-github

Copy link
Copy Markdown
Contributor
<form {...test_form}>
	// not displaying 'default text'
	<p>{test_form.fields.name.value()}</p>

	// displays 'default text'
	<input {...test_form.fields.name.as('text', 'default text')} />
</form>

The fundamental problem with this is that it's impossible to render correctly during SSR. When rendering on the server, the Svelte component is run top to bottom, so the renderer will always hit test_form.fields.name.value(), which is undefined at this point, and then it will later hit the default text bit, which has no way of ever going back and updating the undefined bit. We can get around this on the client by using reactive state, but I feel this points at some larger issue we need to solve around how the "initial state" of forms works.

@filiabel

Copy link
Copy Markdown
Contributor

but I feel this points at some larger issue we need to solve around how the "initial state" of forms works.

I feel like the remote form factory PR is onto something with setting initial data. Also somewhat reminiscent of how Superforms did it.

#14815

@henrykrinkle01

Copy link
Copy Markdown
Contributor

I think we should be able to configure a form instance on the server and set the initial data there, then import it ready-to-use. I put together an example using available tools where I return the form instance with pre-populated data in a remote query:

https://stackblitz.com/edit/remote-form-instance-ji42wvw5?file=src%2Flib%2Fdb.server.ts,src%2Flib%2Fform-instance.svelte.ts,src%2Flib%2Fuser.remote.ts,src%2Fhooks.ts,src%2Froutes%2F[id%3Dint]%2F%2Bpage.svelte

A new remote function for this maybe?

@big-mike-88

Copy link
Copy Markdown

@elliott-with-the-longest-name-on-github

Is this still being looked at or assigned to a milestone? It's a major... I wouldn't say problem, but nuisance, definitely. I realize that this is probably very complicated, as you said, but I wonder if there's an issue or PR on the svelte repo I could follow, just so I can see when my pain will end.

@Rich-Harris

Copy link
Copy Markdown
Member

What's wrong with this? #15707 (comment)

@big-mike-88

Copy link
Copy Markdown

What's wrong with this? #15707 (comment)

It forces me to write code inside the script template when it shouldn't be at all necessary. It also feels unintuitive

If I have something like this: (stupid example I know, just for demo purposes)

Submit your age <input {...fields.age.as('number', 18)}/>

{#if fields.age.value() < 25}
    Eligible for discount!!!
{/if}

The discount banner wouldn't show. It feels like it should. To make it work I basically have to manually set a default in the script, which basically defeats the entire purpose of having a default value in the first place. It would just be a strictly inferior way to assign a default, since you couldn't even use the default value later in the template. Useful only in the scenario where you just want to submit a form and not do anything reactive with the data (which I guess is most scenarios, but it's still annoying)

@Rich-Harris

Copy link
Copy Markdown
Member

The bottom line is we can't merge this PR because of #15690 (comment). So the question becomes how to solve those cases. And as shown, they're already solvable with myform.fields.set(...).

when it shouldn't be at all necessary

Of course it's necessary — any time you need to initialize anything in a component you're going to have to put some code in the <script>. That's what the <script> is for!

  • fields.set({...}) or fields.foo.set('...') — initializes the model
  • fields.foo.as('text', 'default') — creates a fallback for that element when the model is uninitialized, and also controls what happens when the form is reset

These are two different things that you can use separately or together. It feels to me like we're already in good shape here

@big-mike-88

Copy link
Copy Markdown

Apologies, I was just going off of vibes, like how it "feels" it should work. I was unaware of just how unsolvable it is, and since the issue wasn't closed I just assumed that unsolvable meant "difficult and/or low prio". Tone is very difficult to convey via text.

The bottom line is we can't merge this PR because of #15690 (comment).

I mean yeah, if that's the way it works, setting it in the script manually is the way to go. There's nothing wrong with it. This is basically just a doc entry that would need to mention this semantic disconnect between a "default" value and an "initial" value.

@Rich-Harris

Copy link
Copy Markdown
Member

Opened #16145 with a suggested update to the docs

@DrUse1 DrUse1 closed this by deleting the head repository Jun 23, 2026
Rich-Harris added a commit that referenced this pull request Jun 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

forms Stuff relating to forms and form actions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fields.value() returning undefined when input_value is passed to .as()

8 participants