Remix Logo

How can I have structured data in a form?

remix logo

To have structured data in a form, you can use the same key on multiple inputs. For example, if you want to post an array of categories, you can use checkboxes with the same name attribute:

tsx

In your action, you can access the checkbox values using formData.getAll():

tsx

This approach covers most cases for submitting structured data in forms. If you want to submit nested structures as well, you can use non-standard form-field naming conventions and the query-string package from npm:

tsx

In your action, you can parse the form data using request.text() and query-string:

tsx

If you want to send structured data as JSON, you can store the JSON in a hidden field:

tsx

And then parse it in the action:

tsx

Overall, using the same input name and formData.getAll() is often sufficient for submitting structured data in forms.