-
-
Notifications
You must be signed in to change notification settings - Fork 758
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(start): server functions to handle direct submission of
FormData
(
- Loading branch information
1 parent
2466ec1
commit 01a568f
Showing
6 changed files
with
89 additions
and
3 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
e2e/start/basic/app/routes/-server-fns/submit-post-formdata-fn.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { createServerFn } from '@tanstack/start' | ||
|
||
const testValues = { | ||
name: 'Sean', | ||
} | ||
|
||
export const greetUser = createServerFn({ method: 'POST' }) | ||
.validator((data: FormData) => { | ||
if (!(data instanceof FormData)) { | ||
throw new Error('Invalid! FormData is required') | ||
} | ||
const name = data.get('name') | ||
|
||
if (!name) { | ||
throw new Error('Name is required') | ||
} | ||
|
||
return { | ||
name: name.toString(), | ||
} | ||
}) | ||
.handler(({ data: { name } }) => { | ||
return new Response(`Hello, ${name}!`) | ||
}) | ||
|
||
export function SubmitPostFormDataFn() { | ||
return ( | ||
<div className="p-2 border m-2 grid gap-2"> | ||
<h3>Submit POST FormData Fn Call</h3> | ||
<div className="overflow-y-auto"> | ||
It should return navigate and return{' '} | ||
<code> | ||
<pre data-testid="expected-submit-post-formdata-server-fn-result"> | ||
Hello, {testValues.name}! | ||
</pre> | ||
</code> | ||
</div> | ||
<form | ||
className="flex flex-col gap-2" | ||
data-testid="submit-post-formdata-form" | ||
method="POST" | ||
action={greetUser.url} | ||
> | ||
<input type="text" name="name" defaultValue={testValues.name} /> | ||
<button | ||
type="submit" | ||
data-testid="test-submit-post-formdata-fn-calls-btn" | ||
className="rounded-md bg-white px-2.5 py-1.5 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50" | ||
> | ||
Submit (native) | ||
</button> | ||
</form> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters