@fregante/dom-form-serializer 2.1.0-0 → 2.1.0-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +1 -271
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,271 +1 @@
1
- # DOM Form Serializer
2
-
3
- [![CI](https://github.com/jefersondaniel/dom-form-serializer/actions/workflows/ci.yml/badge.svg)](https://github.com/jefersondaniel/dom-form-serializer/actions/workflows/ci.yml)
4
- [![npm version](https://badge.fury.io/js/dom-form-serializer.svg)](https://www.npmjs.com/package/dom-form-serializer)
5
- [![npm](https://img.shields.io/npm/dm/dom-form-serializer.svg)](https://www.npmjs.com/package/dom-form-serializer)
6
-
7
- Serialize forms fields into a JSON representation.
8
-
9
- ## About
10
-
11
- This project is a fork of [Backbone.Syphon](https://github.com/marionettejs/backbone.syphon) that has no dependency on backbone and jquery. It aims to make it easy to serialize the fields of a form into a simple JSON object.
12
-
13
- ### Installing
14
-
15
- ```
16
- npm install dom-form-serializer
17
- ```
18
-
19
- ## Basic Usage
20
-
21
- ### Serialize
22
-
23
- ```js
24
- var serialize = require('dom-form-serializer').serialize
25
- serialize(document.querySelector('#form'))
26
- ```
27
-
28
- ### Keys Retrieved By "name" Attribute
29
-
30
- The default behavior for serializing fields is to use the field's "name" attribute as the key in the serialized object.
31
-
32
- ```html
33
- <form id="form">
34
- <input name="a">
35
- <select name="b"></select>
36
- <textarea name="c"></textarea>
37
- </form>
38
- ```
39
-
40
- ```js
41
- serialize(document.querySelector('#form'))
42
-
43
- // will produce =>
44
-
45
- {
46
- a: "",
47
- b: "",
48
- c: ""
49
- }
50
- ```
51
-
52
- ### Checkboxes
53
-
54
- By default, a checkbox will return a boolean value signifying whether or not it is checked.
55
-
56
- ```html
57
- <form id="form">
58
- <input type="checkbox" name="a">
59
- <input type="checkbox" name="b" checked>
60
- <input type="checkbox" name="c" indeterminate>
61
- </form>
62
- ```
63
-
64
- ```js
65
- serialize(document.querySelector('#form'));
66
-
67
- // will produce =>
68
-
69
- {
70
- a: false,
71
- b: true,
72
- c: null
73
- }
74
- ```
75
-
76
- ### Radio Button Groups
77
-
78
- Radio button groups (grouped by the input element "name" attribute) will produce a single value, from the selected
79
- radio button.
80
-
81
- ```html
82
- <form id="form">
83
- <input type="radio" name="a" value="1">
84
- <input type="radio" name="a" value="2" checked>
85
- <input type="radio" name="a" value="3">
86
- <input type="radio" name="a" value="4">
87
- </form>
88
- ```
89
-
90
- ```js
91
- serialize(document.querySelector('#form'))
92
-
93
- // will produce =>
94
-
95
- {
96
- a: "2"
97
- }
98
- ```
99
-
100
- This behavior can be changed by registering a different set of Key Extractors, Input Readers, and Key Assignment
101
- Validators. See the tests
102
- [serialize.spec.js](https://github.com/jefersondaniel/dom-form-serializer/blob/master/test/serialize.spec.js) for more examples on these.
103
-
104
- ### Drop Down Lists
105
-
106
- Serializing drop down lists (`<select>`) will result in value of the selected option.
107
-
108
-
109
- ```html
110
- <form id="form">
111
- <select name="foo">
112
- <option value="bar"></option>
113
- </select>
114
- </form>
115
- ```
116
-
117
-
118
- ```js
119
- serialize(document.querySelector('#form'))
120
-
121
- // will produce =>
122
-
123
- {
124
- foo: "bar"
125
- }
126
- ```
127
-
128
- ### Multiple Select Boxes
129
-
130
- Serializing multiple select boxes (`<select multiple>`) will yield the selected options as an array.
131
-
132
- ```html
133
- <form id="form">
134
- <select name="foo" multiple>
135
- <option value="foo"></option>
136
- <option value="bar" selected></option>
137
- <option value="baz" selected></option>
138
- </select>
139
- </form>
140
- ```
141
-
142
- ```js
143
- serialize(document.querySelector('#form'))
144
-
145
- // will produce =>
146
-
147
- {
148
- foo: ["bar", "baz"]
149
- }
150
- ```
151
-
152
- ## Basic Usage: Deserialize
153
-
154
- You can also deserialize an object's values back into their field equivalent. It uses the same conventions and configuration as the serialization process, with the introduction of Input Writers to handle populating the fields with the values
155
-
156
- ```html
157
- <form id="form">
158
- <input type="text" name="a">
159
- <input type="text" name="b">
160
- </form>
161
- ```
162
-
163
- ```js
164
- var data = {
165
- a: "foo",
166
- b: "bar"
167
- };
168
-
169
- deserialize(document.querySelector('#form'), data);
170
- ```
171
-
172
- This will populate the form field elements with the correct values from the `data` parameter.
173
-
174
- ## Ignored Input Types
175
-
176
- The following types of input are ignored, and not included in the resulting JavaScript object:
177
-
178
- * `<input type="submit">` buttons
179
- * `<input type="reset"`> buttons
180
- * standard `<button>` tags
181
-
182
- If you need to get a value from the specific button that was clicked, you can use a DOM event to listen for that element being manipulated (clicked, for example) and manually grab
183
- the data you need.
184
-
185
- ### Ignoring Other Input Types
186
-
187
- You can define ignored selectors using the ignoredTypes option.
188
-
189
- ```js
190
- // ignore all <textarea> input elements
191
- serialize(element, {ignoredTypes: ['textarea']})
192
- ```
193
-
194
- ## Serializing Nested Attributes And Field Names
195
-
196
- `serialize` will parse nested attribute names and create a nested result object, using the Rails standard of `name="foo[bar][baz]"` by default.
197
-
198
- ```html
199
- <form>
200
- <input type="text" name="foo[bar]" value="a value">
201
- <input type="text" name="foo[baz][quux]" value="another value">
202
- </form>
203
- ```
204
-
205
- will produce
206
-
207
- ```js
208
- {
209
- foo: {
210
- bar: "a value",
211
- baz: {
212
- quux: "another value"
213
- }
214
- }
215
- }
216
- ```
217
-
218
- ### Array Inputs
219
-
220
- `serialize` will parse multiple inputs named after the convention `name="foo[bar][]"` into elements of the array `bar`.
221
-
222
- ```html
223
- <form>
224
- <input type="checkbox" name="foo[bar][]" value="baz" checked="checked">
225
- <input type="checkbox" name="foo[bar][]" value="qux" checked="checked">
226
- </form>
227
- ```
228
-
229
- will produce
230
-
231
- ```js
232
- {
233
- foo: {
234
- bar: ["baz", "qux"]
235
- }
236
- }
237
- ```
238
-
239
- ### Custom splitters
240
-
241
- If your keys are split by something else than the Rails Array convention (for example `name="foo.bar.quux"`), you may pass this delimiter into `serialize` using the `keySplitter` option.
242
-
243
- ```html
244
- <form id="form">
245
- <input type="text" name="widget" value="wombat">
246
- <input type="text" name="foo.bar" value="baz">
247
- <input type="text" name="foo.baz.quux" value="qux">
248
- </form>
249
- ```
250
-
251
- ```js
252
- serialize(document.querySelector('#form'), { keySplitter: key => key.split('.') })
253
-
254
- // will produce =>
255
-
256
- {
257
- widget: "wombat",
258
- foo: {
259
- bar: "baz",
260
- baz: {
261
- quux: "qux"
262
- }
263
- }
264
- }
265
-
266
- ```
267
-
268
- # Acknowledgments
269
-
270
- [Backbone.Syphon](https://github.com/marionettejs/backbone.syphon)
271
-
1
+ This fork only exists until the changes in this pull request are merged: https://github.com/jefersondaniel/dom-form-serializer/pull/37
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@fregante/dom-form-serializer",
3
- "version": "2.1.0-0",
4
- "description": "Serialize form inputs",
3
+ "version": "2.1.0-1",
4
+ "description": "Temporary fork",
5
5
  "main": "dist/dom-form-serializer.js",
6
6
  "module": "dist/dom-form-serializer.mjs",
7
7
  "files": [