Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/olive-icons-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: `fields.value()` initial data missing
14 changes: 6 additions & 8 deletions packages/kit/src/runtime/app/server/remote/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,24 +197,22 @@ export function form(validate_or_fn, maybe_fn) {

Object.defineProperty(instance, 'fields', {
get() {
const data = get_cache(__)?.[''];
const issues = flatten_issues(data?.issues ?? []);

return create_field_proxy(
{},
() => data?.input ?? {},
() => (get_cache(__)[''] ?? {}).input ?? {},
(path, value) => {
if (data?.submission) {
const data = (get_cache(__)[''] ??= {});
if (data.submission) {
// don't override a submission
return;
}

const input =
path.length === 0 ? value : deep_set(data?.input ?? {}, path.map(String), value);
path.length === 0 ? value : deep_set(data.input ?? {}, path.map(String), value);

(get_cache(__)[''] ??= {}).input = input;
data.input = input;
},
() => issues
() => flatten_issues((get_cache(__)[''] ?? {}).issues ?? [])
);
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
import { values } from './value.remote.js';
const fields = values.fields;
const initial_value = $state({ val: 'initial' });
fields.set(initial_value);
</script>

<h1>Remote Form Initial Value Test</h1>

<pre id="initial">Initial: {JSON.stringify(initial_value)}</pre>
<pre id="value">Value: {JSON.stringify(fields.value())}</pre>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const csr = false;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { form } from '$app/server';
import * as v from 'valibot';

export const values = form(
v.object({
val: v.string()
}),
async (data) => {
return { success: true, data };
}
);
9 changes: 9 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1790,7 +1790,7 @@
await expect(page.locator('[data-scoped] input')).toHaveValue('');
});

test('form enhance(...) works', async ({ page, javaScriptEnabled }) => {

Check warning on line 1793 in packages/kit/test/apps/basics/test/test.js

View workflow job for this annotation

GitHub Actions / test-kit (18, ubuntu-latest, chromium)

flaky test: form enhance(...) works

retries: 2
await page.goto('/remote/form');

await page.fill('[data-enhanced] input', 'hello');
Expand Down Expand Up @@ -1818,6 +1818,15 @@
);
});

test('form initial value works', async ({ page, javaScriptEnabled }) => {
if (javaScriptEnabled) return;

await page.goto('/remote/form/initial-value');

await expect(page.locator('#initial')).toHaveText('Initial: {"val":"initial"}');
await expect(page.locator('#value')).toHaveText('Value: {"val":"initial"}');
});

test('form preflight works', async ({ page, javaScriptEnabled }) => {
if (!javaScriptEnabled) return;

Expand Down
Loading