|
| 1 | +--- |
| 2 | +title: Form Data Helpers |
| 3 | +description: Utilities for parsing form data and handling validation errors with bracket notation support. |
| 4 | +--- |
| 5 | + |
| 6 | +# Form Data Helpers |
| 7 | + |
| 8 | +Form data helpers provide utilities for parsing HTML form data and extracting validation error messages, with full support for [bracket notation](/docs/openapi/bracket-notation) to handle complex nested structures. |
| 9 | + |
| 10 | +## `parseFormData` |
| 11 | + |
| 12 | +Parses HTML form data using [bracket notation](/docs/openapi/bracket-notation) to deserialize complex nested objects and arrays. |
| 13 | + |
| 14 | +```ts twoslash |
| 15 | +import { parseFormData } from '@orpc/openapi-client/helpers' |
| 16 | + |
| 17 | +const form = new FormData() |
| 18 | +form.append('name', 'John') |
| 19 | +form.append('user[email]', 'john@example.com') |
| 20 | +form.append('user[hobbies][]', 'reading') |
| 21 | +form.append('user[hobbies][]', 'gaming') |
| 22 | + |
| 23 | +const parsed = parseFormData(form) |
| 24 | +// Result: |
| 25 | +// { |
| 26 | +// name: 'John', |
| 27 | +// user: { |
| 28 | +// email: 'john@example.com', |
| 29 | +// hobbies: ['reading', 'gaming'] |
| 30 | +// } |
| 31 | +// } |
| 32 | +``` |
| 33 | + |
| 34 | +## `getIssueMessage` |
| 35 | + |
| 36 | +Extracts validation error messages from [standard schema](https://github.com/standard-schema/standard-schema) issues using [bracket notation](/docs/openapi/bracket-notation) paths. |
| 37 | + |
| 38 | +```ts twoslash |
| 39 | +import { getIssueMessage } from '@orpc/openapi-client/helpers' |
| 40 | + |
| 41 | +const error = { |
| 42 | + data: { |
| 43 | + issues: [ |
| 44 | + { |
| 45 | + path: ['user', 'email'], |
| 46 | + message: 'Invalid email format' |
| 47 | + } |
| 48 | + ] |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +const emailError = getIssueMessage(error, 'user[email]') |
| 53 | +// Returns: 'Invalid email format' |
| 54 | + |
| 55 | +const tagError = getIssueMessage(error, 'user[tags][]') |
| 56 | +// Returns error message for any array item |
| 57 | + |
| 58 | +const anyError = getIssueMessage('anything', 'path') |
| 59 | +// Returns undefined if cannot find issue |
| 60 | +``` |
| 61 | + |
| 62 | +::: warning |
| 63 | +The `getIssueMessage` utility works with any data type but requires validation errors to follow the [standard schema issue format](https://github.com/standard-schema/standard-schema?tab=readme-ov-file#the-interface). It looks for issues in the `data.issues` property. If you use custom [validation errors](/docs/advanced/validation-errors), store them elsewhere, or modify the issue format, `getIssueMessage` may not work as expected. |
| 64 | +::: |
| 65 | + |
| 66 | +## Usage Example |
| 67 | + |
| 68 | +```tsx |
| 69 | +import { getIssueMessage, parseFormData } from '@orpc/openapi-client/helpers' |
| 70 | + |
| 71 | +export function ContactForm() { |
| 72 | + const [error, setError] = useState() |
| 73 | + |
| 74 | + const handleSubmit = (form: FormData) => { |
| 75 | + try { |
| 76 | + const data = parseFormData(form) |
| 77 | + // Process structured data |
| 78 | + } |
| 79 | + catch (error) { |
| 80 | + setError(error) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return ( |
| 85 | + <form action={handleSubmit}> |
| 86 | + <input name="user[name]" type="text" /> |
| 87 | + <span>{getIssueMessage(error, 'user[name]')}</span> |
| 88 | + |
| 89 | + <input name="user[emails][]" type="email" /> |
| 90 | + <span>{getIssueMessage(error, 'user[emails][]')}</span> |
| 91 | + |
| 92 | + <button type="submit">Submit</button> |
| 93 | + </form> |
| 94 | + ) |
| 95 | +} |
| 96 | +``` |
0 commit comments