form.mdx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import Tabs from '@theme/Tabs';
  2. import TabItem from '@theme/TabItem';
  3. import UppyCdnExample from '/src/components/UppyCdnExample';
  4. # Form
  5. The `@uppy/form` plugin integrates with an existing HTML `<form>` element to
  6. extract input data from it, and send along with the Uppy upload. It then appends
  7. the upload result back to the form via a hidden input.
  8. ## When should I use this?
  9. When you have an existing HTML `<form>` element and you would like to:
  10. - **Attach the form input values to the files.** This is useful if you want to
  11. associate meta data from the form (for example, name, location, id) with the
  12. uploaded file, so you can process it on the backend. `@uppy/form` extracts the
  13. input values before uploading/processing files and adds them to Uppy meta data
  14. state (`uppy.state.meta`), as well as and each file’s meta, and appends to the
  15. upload in an object with `[file input name attribute]` -> `[file input value]`
  16. key/values.
  17. - **Upload the files and put the response (such as the file URLs) into a hidden
  18. field** (`<input type="hidden" name="uppyResult">`). Then you can POST and
  19. handle the form yourself. The appended result is a stringified version of a
  20. result returned from calling `uppy.upload()` or listening to `complete` event.
  21. - **Automatically start the file upload on submit or submit the form after file
  22. upload.** This is off by default. See [`submitOnSuccess`](#submitOnSuccess)
  23. and [`triggerUploadOnSubmit`](#triggerUploadOnSubmit) options respectively for
  24. details.
  25. :::note
  26. If you are using a UI framework or library like React, Vue or Svelte, you’ll
  27. most likely handle form data there as well, and thus won’t need this plugin.
  28. Instead, pass meta data to Uppy via [`uppy.setMeta()`](/docs/uppy#setmetadata)
  29. and listen to [`uppy.on('complete')`](/docs/uppy#complete) to get the upload
  30. results back.
  31. :::
  32. ## Install
  33. <Tabs>
  34. <TabItem value="npm" label="NPM" default>
  35. ```shell
  36. npm install @uppy/form
  37. ```
  38. </TabItem>
  39. <TabItem value="yarn" label="Yarn">
  40. ```shell
  41. yarn add @uppy/form
  42. ```
  43. </TabItem>
  44. <TabItem value="cdn" label="CDN">
  45. <UppyCdnExample>
  46. {`
  47. import { Uppy, Form } from "{{UPPY_JS_URL}}"
  48. const uppy = new Uppy()
  49. uppy.use(Form, {
  50. // Options
  51. })
  52. `}
  53. </UppyCdnExample>
  54. </TabItem>
  55. </Tabs>
  56. ## Use
  57. ```js title="app.js"
  58. import Uppy from '@uppy/core';
  59. import Form from '@uppy/form';
  60. import DragDrop from '@uppy/drag-drop';
  61. import '@uppy/core/dist/style.min.css';
  62. import '@uppy/drag-drop/dist/style.min.css';
  63. new Uppy().use(Form, {
  64. target: '#my-form',
  65. });
  66. ```
  67. ```html title="index.html"
  68. <form id="my-form" action="/submit" method="get">
  69. <label for="name">Enter your name: </label>
  70. <input type="text" name="name" required />
  71. <label for="dob">Date of birth: </label>
  72. <input type="date" name="dob" />
  73. <input type="submit" value="Save" />
  74. </form>
  75. ```
  76. By default the code above will:
  77. 1. Extract meta data from the form `#my-form`.
  78. 2. Send it with the Uppy upload.
  79. 3. Those fields will then be added to Uppy meta data state (`uppy.state.meta`)
  80. and each file’s meta, and appended as (meta)data to the upload in an object
  81. with `[file input name attribute]` -> `[file input value]` key/values.
  82. 4. When Uppy completes upload/processing, it will add an
  83. `<input type="hidden" name="uppyResult">` with the stringified upload result
  84. object back to the form.
  85. :::note
  86. You can disable both of these features, see options below.
  87. :::
  88. :::tip
  89. `@uppy/form` can also start Uppy upload automatically once the form is
  90. submitted, and even submit the form after the upload is complete. This is off by
  91. default. See [`triggerUploadOnSubmit`](#triggerUploadOnSubmit) and
  92. [`submitOnSuccess`](#submitOnSuccess) options below for details.
  93. :::
  94. ## API
  95. ### Options
  96. #### `id`
  97. A unique identifier for this plugin (`string`, default: `'Form'`).
  98. #### `target`
  99. DOM element or CSS selector for the form element (`string` or `Element`,
  100. default: `null`).
  101. This is required for the plugin to work.
  102. #### `resultName`
  103. The `name` attribute for the `<input type="hidden">` where the result will be
  104. added (`string`, default: `uppyResult`).
  105. #### `getMetaFromForm`
  106. Configures whether to extract metadata from the form (`boolean`, default:
  107. `true`).
  108. When set to `true`, the Form plugin will extract all fields from a `<form>`
  109. element before upload begins. Those fields will then be added to Uppy meta data
  110. state (`uppy.state.meta`) and each file’s meta, and appended as (meta)data to
  111. the upload in an object with `[file input name attribute]` ->
  112. `[file input value]` key/values.
  113. #### `addResultToForm`
  114. Configures whether to add upload/encoding results back to the form in an
  115. `<input name="uppyResult" type="hidden">` element (`boolean`, default: `true`).
  116. #### `triggerUploadOnSubmit`
  117. Configures whether to start the upload when the form is submitted (`boolean`,
  118. default: `false`).
  119. When a user submits the form (via a submit button, the <kbd>Enter</kbd> key or
  120. otherwise), this option will prevent form submission, and instead upload files
  121. via Uppy. Then you could:
  122. - Set `submitOnSuccess: true` if you need the form to _actually_ be submitted
  123. once all files have been uploaded.
  124. - Listen for `uppy.on('complete')` event to do something else if the file
  125. uploads are all you need. For example, if the form is used for file metadata
  126. only.
  127. #### `submitOnSuccess`
  128. Configures whether to submit the form after Uppy finishes uploading/encoding
  129. (`boolean`, default: `false`).