{"id":17436148,"url":"https://github.com/rory660/deepforms","last_synced_at":"2025-04-16T03:34:52.402Z","repository":{"id":42879333,"uuid":"255642321","full_name":"rory660/deepforms","owner":"rory660","description":"Send deep nested JSON-encoded objects from HTML forms","archived":false,"fork":false,"pushed_at":"2022-12-30T20:16:05.000Z","size":257,"stargazers_count":13,"open_issues_count":13,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-29T04:42:25.916Z","etag":null,"topics":["forms","html","javascript","json","nested-objects","node-js"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rory660.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-14T15:06:44.000Z","updated_at":"2023-06-24T14:34:12.000Z","dependencies_parsed_at":"2023-01-31T15:25:13.195Z","dependency_job_id":null,"html_url":"https://github.com/rory660/deepforms","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rory660%2Fdeepforms","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rory660%2Fdeepforms/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rory660%2Fdeepforms/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rory660%2Fdeepforms/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rory660","download_url":"https://codeload.github.com/rory660/deepforms/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249191341,"owners_count":21227547,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["forms","html","javascript","json","nested-objects","node-js"],"created_at":"2024-10-17T10:03:58.489Z","updated_at":"2025-04-16T03:34:52.375Z","avatar_url":"https://github.com/rory660.png","language":"JavaScript","readme":"[![deepforms](https://github.com/rory660/deepforms/blob/master/img/deepformsCentered.png?raw=true)](#)\n\n[![License: MIT](https://img.shields.io/badge/license-MIT-informational)](#) [![dependencies: None](https://img.shields.io/badge/dependencies-none-success)](#) [![CircleCI](https://circleci.com/gh/rory660/deepforms.svg?style=shield\u0026circle-token=d5e081f54b8a54d62f5b2c213a1a88a1399786fc)](https://app.circleci.com/pipelines/github/rory660/deepforms)\n\nSend deep nested JSON-encoded objects from HTML forms, in a single function call. [Check it out on npm!](https://www.npmjs.com/package/deepforms)\n\n# Installation\n\n_In the browser:_\n\nInclude the script from a node CDN such as _jsdelivr_:\n```html\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/deepforms@1.0.0/deepforms.min.js\"\u003e\u003c/script\u003e\n```\n\n---\n\n_As an Express middleware:_\n\nrun `npm install deepforms` in your package and place `require(\"deepforms\")` in your node script.\n\n# Features\n\n- Allows forms to be sent as JSON strings representing deep objects\n- Aggregates data from form fields that share a name attribute into a list\n- Can be imported as an express middleware in node.js to parse the sent JSON\n- Submits form data with the same method, action, target, etc as the original form, with no extra code required\n\n# API\n\n_When imported as a frontend JS library:_\n\n### `submitDeepForm(formId)`\n\nArguments : The id of the form element to be submitted.\n\nSubmits the form as a deep nested JSON string containing the form data.\n\n---\n\n_When imported as a Node.js module:_\n\n### `parser(req, res, next)`\n\nArguments: request object, response object, next function in middleware chain\n\nExpress middleware that parses the deep form JSON string from `req.body.deepFormJSON`, if it exists, and places the parsed object in `req.deepFormData`.\n\n# Usage\n\n## Sending Forms\n\nA form can be sent as a deep form by invoking `window.deepforms.submitDeepForm()`. Deep form data will be submitted as a single key `\"deepFormJSON\"` mapped to a JSON string containing the deep object.\n\n### Field names\n\nForm field names can be written using '.' syntax to represent layers of nesting in an object. For example:\n\n```html\n\u003cinput type=\"text\" name=\"user.name\" value=\"testUser\"\u003e\n```\n\nwill be entered into the form object as:\n\n```js\ndeepFormObject[\"user\"][\"name\"] = \"testUser\";\n```\n\n### Multiple fields sharing the same name\n\nIf multiple fields in the form share a name, then their values will be parsed as a list:\n\n```html\n\u003cinput type=\"text\" name=\"color\" value=\"blue\"\u003e\n\u003cinput type=\"text\" name=\"color\" value=\"red\"\u003e\n```\n\nwill be entered into the form object as:\n\n```js\ndeepFormObject[\"color\"] = [\"blue\", \"red\"];\n```\n\n### `submitDeepForm()`\n\nIn order to submit a form as a deep form, invoke `submitDeepForm` as follows:\n\n```html\n\u003cform id=\"exampleForm\" method=\"POST\" action=\"/submitForm\"\u003e\n    \u003cinput type=\"text\" name=\"user.name\" value=\"testUser\"\u003e\n    \u003cinput type=\"text\" name=\"color\" value=\"blue\"\u003e\n    \u003cinput type=\"text\" name=\"color\" value=\"red\"\u003e\n\u003c/form\u003e\n\u003cbutton onclick=\"document.deepforms.submitDeepForm('exampleForm')\"\u003e\n```\n\nClicking the button will cause the form to be parsed as an object:\n\n```json\n{\n    \"user\" : {\n        \"name\" : \"testUser\"\n    },\n    \"color\" : [\"blue\", \"red\"]\n}\n```\n\nand sent as a JSON string to the \"/submitForm\" route. Deep form data is always sent as a key/value pair with the key `\"deepFormJSON\"` and the JSON string as the value. The example above would be sent as:\n\n```\ndeepFormJSON : '{\"user\":{\"name\":\"testUser\"},\"color\":[\"blue\",\"red\"]}}'\n```\n\n---\n\n## WARNING!\nWhen you call `submitDeepForm` from an element's onclick event, make sure that the element is either:\n\n- __Not__ contained within the form.\n- Has its `type` field set to something other than `\"submit\"`.\n\nButton elements inside forms will default to `type=\"submit\"`, causing them to submit the form normally, rather than through the library function.\n\n---\n\n## Parsing Forms\n\nForm data is always sent as the following key/value pair:\n\n```\ndeepFormJSON = \u003cJSON string containing form data\u003e\n```\n\n### Express Middleware\n\nImporting deepforms as a Node.js module exposes the `parser` middleware, which can be used in Express servers:\n\n```js\nconst express = require(\"express\")\nconst deepforms = require(\"deepforms\")\n\nconst app = express()\n\napp.use(deepforms.parser)\n\n```\n\nWhen the deep form parser is set as an Express middleware, any deep form sent to the server will be automatically parsed as an object and placed in `req.deepFormData`.\n\nWith the earlier example of `user.name = \"testUser\"` and `color = [\"blue\", \"red\"]`, the parser would cause `req` to contain the following:\n\n```js\nreq.deepFormData == {\n    \"user\" : {\n        \"name\" : \"testUser\"\n    },\n    \"color\" : [\"blue\", \"red\"]\n};\n```\n\n### Parsing the JSON directly\n\nThe JSON string can be found in the `deepFormJSON` field of the request body (or URL parameter if a GET request is sent). It can be simply parsed through `JSON.parse`:\n\n```js\nconst deepFormData = JSON.parse(req.body.deepFormJSON)\n```\n\n## Author\n\n[deepforms is created and maintained by Rory Brown](https://www.rorybrown.info)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frory660%2Fdeepforms","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frory660%2Fdeepforms","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frory660%2Fdeepforms/lists"}