Skip to content

Commit e56b39b

Browse files
committed
fix: fields.value() initial data missing
1 parent ab37849 commit e56b39b

File tree

6 files changed

+43
-8
lines changed

6 files changed

+43
-8
lines changed

.changeset/olive-icons-jump.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: `fields.value()` initial data missing

packages/kit/src/runtime/app/server/remote/form.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,24 +197,22 @@ export function form(validate_or_fn, maybe_fn) {
197197

198198
Object.defineProperty(instance, 'fields', {
199199
get() {
200-
const data = get_cache(__)?.[''];
201-
const issues = flatten_issues(data?.issues ?? []);
202-
203200
return create_field_proxy(
204201
{},
205-
() => data?.input ?? {},
202+
() => (get_cache(__)[''] ?? {}).input ?? {},
206203
(path, value) => {
207-
if (data?.submission) {
204+
const data = (get_cache(__)[''] ??= {});
205+
if (data.submission) {
208206
// don't override a submission
209207
return;
210208
}
211209

212210
const input =
213-
path.length === 0 ? value : deep_set(data?.input ?? {}, path.map(String), value);
211+
path.length === 0 ? value : deep_set(data.input ?? {}, path.map(String), value);
214212

215-
(get_cache(__)[''] ??= {}).input = input;
213+
data.input = input;
216214
},
217-
() => issues
215+
() => flatten_issues((get_cache(__)[''] ?? {}).issues ?? [])
218216
);
219217
}
220218
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
import { values } from './value.remote.js';
3+
const fields = values.fields;
4+
const initial_value = $state({ val: 'initial' });
5+
fields.set(initial_value);
6+
</script>
7+
8+
<h1>Remote Form Initial Value Test</h1>
9+
10+
<pre id="initial">Initial: {JSON.stringify(initial_value)}</pre>
11+
<pre id="value">Value: {JSON.stringify(fields.value())}</pre>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const csr = false;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { form } from '$app/server';
2+
import * as v from 'valibot';
3+
4+
export const values = form(
5+
v.object({
6+
val: v.string()
7+
}),
8+
async (data) => {
9+
return { success: true, data };
10+
}
11+
);

packages/kit/test/apps/basics/test/test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,6 +1818,15 @@ test.describe('remote functions', () => {
18181818
);
18191819
});
18201820

1821+
test('form initial value works', async ({ page, javaScriptEnabled }) => {
1822+
if (javaScriptEnabled) return;
1823+
1824+
await page.goto('/remote/form/initial-value');
1825+
1826+
await expect(page.locator('#initial')).toHaveText('Initial: {"val":"initial"}');
1827+
await expect(page.locator('#value')).toHaveText('Value: {"val":"initial"}');
1828+
});
1829+
18211830
test('form preflight works', async ({ page, javaScriptEnabled }) => {
18221831
if (!javaScriptEnabled) return;
18231832

0 commit comments

Comments
 (0)