Create FormData
Converts JavaScript objects, nested structures, and files into standard FormData with comprehensive formatting options.
createFormData
Converts JavaScript objects into FormData with extensive configuration options for handling nested structures, files, and case transformations.
Function Signature
function createFormData<T extends GenericObject>(
data: T,
configs?: FormDataConfigs<T>
): FormDataParameters
| Parameter | Type | Description |
|---|---|---|
data | T | The source object to convert to FormData. |
configs | FormDataConfigs<T> (optional) | Configuration options controlling serialization. |
Returns
FormData: A standardFormDatainstance containing the mapped inputs.
Example Usage
playground.ts
Behavior Details
Nested Objects
- By default, nested objects are JSON-stringified (
JSON.stringify) which is standard for modern API submissions. - Pass keys to
dotNotateNestedto flatten them recursively into dot-notated parameters (e.g.user.address.zip).
File Uploads
- Automatically handles standard
File,Blob,FileList, and file-upload formats from common UI Component Libraries (such as Ant Design).
Case & Whitespace Control
- Trims all string values when
trimStringsistrue. - Converts specific key paths or values to lowercase using
lowerCaseKeysandlowerCaseValues.
Warning
Server-Side Rendering (SSR): This utility is designed for browser environments and may fail or produce unexpected results when run inside server-side environments (such as Next.js Server Components) due to server-side FormData limitations.
Aliases
This function can also be imported under the following name:
| Alias | Import Path |
|---|---|
convertToFormData | toolbox-x/dom |
Types
FormDataConfigs
interface FormDataConfigs<T> {
ignoreKeys?: DotNotationKey<T>[];
requiredKeys?: '*' | DotNotationKey<T>[];
lowerCaseKeys?: '*' | DotNotationKey<T>[];
lowerCaseValues?: '*' | NestedKeyString<T>[];
dotNotateNested?: '*' | KeyForObject<T>[];
stringifyNested?: '*' | KeyForObject<T>[];
breakArray?: '*' | KeyForArray<T>[];
trimStrings?: boolean;
}Last updated: Mon, Jun 15, 2026 11:04:47AM (UTC)
