Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/pink-ghosts-see.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: require a value with `submit` and `hidden` fields
10 changes: 7 additions & 3 deletions packages/kit/src/exports/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1850,19 +1850,23 @@ export type RemoteFormFieldType<T> = {
// Input element properties based on type
type InputElementProps<T extends keyof InputTypeMap> = T extends 'checkbox' | 'radio'
? {
name: string;
type: T;
value?: string;
'aria-invalid': boolean | 'false' | 'true' | undefined;
get checked(): boolean;
set checked(value: boolean);
}
: T extends 'file'
? {
name: string;
type: 'file';
'aria-invalid': boolean | 'false' | 'true' | undefined;
get files(): FileList | null;
set files(v: FileList | null);
}
: {
name: string;
type: T;
'aria-invalid': boolean | 'false' | 'true' | undefined;
get value(): string | number;
Expand All @@ -1882,10 +1886,10 @@ export type RemoteFormFieldValue = string | string[] | number | boolean | File |

type AsArgs<Type extends keyof InputTypeMap, Value> = Type extends 'checkbox'
? Value extends string[]
? [type: 'checkbox', value: Value[number] | (string & {})]
? [type: Type, value: Value[number] | (string & {})]
: [type: Type]
: Type extends 'radio'
? [type: 'radio', value: Value | (string & {})]
: Type extends 'radio' | 'submit' | 'hidden'
? [type: Type, value: Value | (string & {})]
: [type: Type];

/**
Expand Down
13 changes: 13 additions & 0 deletions packages/kit/src/runtime/form-utils.svelte.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,19 @@ export function create_field_proxy(target, get_input, set_input, get_issues, pat
base_props.type = type === 'file multiple' ? 'file' : type;
}

// Handle submit and hidden inputs
if (type === 'submit' || type === 'hidden') {
if (DEV) {
if (!input_value) {
throw new Error(`\`${type}\` inputs must have a value`);
}
}

return Object.defineProperties(base_props, {
value: { value: input_value, enumerable: true }
});
}

// Handle select inputs
if (type === 'select' || type === 'select multiple') {
return Object.defineProperties(base_props, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
</script>

<form {...my_form}>
<button {...my_form.fields.submitter.as('submit')} value="hello">submit</button>
<button {...my_form.fields.submitter.as('submit', 'hello')}>submit</button>
</form>

<p id="result">{my_form.result}</p>
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

<button>submit (imperative validation)</button>

<button bind:this={submitter} {...my_form.fields.button.as('submit')} value="incorrect_value">
<button bind:this={submitter} {...my_form.fields.button.as('submit', 'incorrect_value')}>
submit
</button>
{#each my_form.fields.button.issues() as issue}
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1600,7 +1600,7 @@ test.describe('getRequestEvent', () => {
});
});

test.describe('remote functions', () => {
test.describe.only('remote functions', () => {
test('query returns correct data', async ({ page, javaScriptEnabled }) => {
await page.goto('/remote');
await expect(page.locator('#echo-result')).toHaveText('Hello world');
Expand Down
10 changes: 7 additions & 3 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1826,19 +1826,23 @@ declare module '@sveltejs/kit' {
// Input element properties based on type
type InputElementProps<T extends keyof InputTypeMap> = T extends 'checkbox' | 'radio'
? {
name: string;
type: T;
value?: string;
'aria-invalid': boolean | 'false' | 'true' | undefined;
get checked(): boolean;
set checked(value: boolean);
}
: T extends 'file'
? {
name: string;
type: 'file';
'aria-invalid': boolean | 'false' | 'true' | undefined;
get files(): FileList | null;
set files(v: FileList | null);
}
: {
name: string;
type: T;
'aria-invalid': boolean | 'false' | 'true' | undefined;
get value(): string | number;
Expand All @@ -1858,10 +1862,10 @@ declare module '@sveltejs/kit' {

type AsArgs<Type extends keyof InputTypeMap, Value> = Type extends 'checkbox'
? Value extends string[]
? [type: 'checkbox', value: Value[number] | (string & {})]
? [type: Type, value: Value[number] | (string & {})]
: [type: Type]
: Type extends 'radio'
? [type: 'radio', value: Value | (string & {})]
: Type extends 'radio' | 'submit' | 'hidden'
? [type: Type, value: Value | (string & {})]
: [type: Type];

/**
Expand Down
Loading