From 72fbd4c99a7b7dd84c2f86d8f1d80977dbde45d8 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Sun, 12 Jan 2025 23:55:58 +0000 Subject: [PATCH] fix(start): preserve multiple values for same key in `FormData` serialization (#3140) Co-authored-by: SeanCassiere <33615041+SeanCassiere@users.noreply.github.com> --- .../serialize-formdata-fn-call.tsx | 19 ++++--- packages/react-router/src/index.tsx | 1 + packages/react-router/src/transformer.ts | 52 +++++++++++++------ .../react-router/tests/transformer.test.tsx | 20 +++++++ 4 files changed, 71 insertions(+), 21 deletions(-) diff --git a/e2e/start/basic/app/routes/-server-fns/serialize-formdata-fn-call.tsx b/e2e/start/basic/app/routes/-server-fns/serialize-formdata-fn-call.tsx index c33ff93686..d6bcc56d17 100644 --- a/e2e/start/basic/app/routes/-server-fns/serialize-formdata-fn-call.tsx +++ b/e2e/start/basic/app/routes/-server-fns/serialize-formdata-fn-call.tsx @@ -4,28 +4,32 @@ import { createServerFn } from '@tanstack/start' const testValues = { name: 'Sean', age: 25, + pet1: 'dog', + pet2: 'cat', __adder: 1, } export const greetUser = createServerFn() - .validator((data: unknown) => { + .validator((data: FormData) => { if (!(data instanceof FormData)) { throw new Error('Invalid! FormData is required') } const name = data.get('name') const age = data.get('age') + const pets = data.getAll('pet') - if (!name || !age) { - throw new Error('Name and age are required') + if (!name || !age || pets.length === 0) { + throw new Error('Name, age and pets are required') } return { name: name.toString(), age: parseInt(age.toString(), 10), + pets: pets.map((pet) => pet.toString()), } }) - .handler(async ({ data: { name, age } }) => { - return `Hello, ${name}! You are ${age + testValues.__adder} years old.` + .handler(({ data: { name, age, pets } }) => { + return `Hello, ${name}! You are ${age + testValues.__adder} years old, and your favorite pets are ${pets.join(',')}.` }) // Usage @@ -40,7 +44,8 @@ export function SerializeFormDataFnCall() {
             Hello, {testValues.name}! You are{' '}
-            {testValues.age + testValues.__adder} years old.
+            {testValues.age + testValues.__adder} years old, and your favorite{' '}
+            pets are {testValues.pet1},{testValues.pet2}.
           
@@ -55,6 +60,8 @@ export function SerializeFormDataFnCall() { > + +