未被触及的远程表单 `<select>` 在 `onchange` 上运行 `validate()` 时呈现为空白
作者: CheHyeonYeong创建于 2026年9月18日更新于 2026年9月18日
schema.ts
import { object, optional, string } from 'valibot';
export const MreSchema = object({
picked: optional(string(), ''),
text: optional(string(), ''),
});data.remote.ts
import { form } from '$app/server';
import { MreSchema } from './schema.ts';
export const mreForm = form(MreSchema, async () => {
return { ok: true };
});+page.svelte
<script lang="ts">
import { mreForm as _mreForm } from './data.remote.ts';
import { MreSchema } from './schema.ts';
const uid = $props.id();
const mreForm = _mreForm.for(uid);
</script>
<form {...mreForm.preflight(MreSchema)} onchange={() => mreForm.validate()}>
<select {...mreForm.fields.picked.as('select')}>
<option value="">-- pick --</option>
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
</select>
<input {...mreForm.fields.text.as('text')} placeholder="type here, then blur" />
<button>submit</button>
</form>Steps (do not touch the <select>):
- The
<select>shows-- pick --. - Type into the text input and blur it — this fires
onchange→validate(). - The untouched
<select>now displays blank (itsselectedIndexis-1) instead of-- pick --.
Removing onchange={() => mreForm.validate()} leaves the <select> alone.
内容来源: sveltejs/kit