question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fix(Form): Form doesn't serialize array form fields

See original GitHub issue

Steps to Reproduce

  1. Build a form with array name fields (data[one], data[two])
<Form.Input name="data[one]"></Form.Input>
<Form.Input name="data[two]"></Form.Input>

Expected Form should serialize those fields into a single object

formData: {
  data: {
    one: 'value',
    two: 'value'
  }
}

Result Form does this:

formData : {
  data[one]: 'value',
  data[two]: 'value'
}

Testcase http://codepen.io/anon/pen/VmxEKQ (see output in console)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
levithomasoncommented, Dec 9, 2016

All form serialization and validation will be accomplished using the controlled component pattern where the value of each form control is captured and placed in state somewhere, probably a higher order form component. You can refer to react-reformed for some ideas.

We’ll likely bake something like this into SUIR and attempt to simplify the API and usage a bit to reduce the workload of wiring this up.

So, in that case, instead of using the name attribute in the DOM to find values and serialize them, your virtual DOM change handlers will be placing the value in the correct place in the form’s state model. This is preferred since the data is then handled inside of React, instead of the browser’s real DOM. Aside from the fact that using the real DOM is an antipattern, using React’s virtual DOM makes testing a breeze, keeps the app fast, and is better for integrating other tools like validation and local storage.

You might also find some of these other react form libraries helpful: https://github.com/erikras/redux-form/ https://github.com/christianalfoni/formsy-react https://github.com/prometheusresearch/react-forms

Cheers!

0reactions
levithomasoncommented, Dec 9, 2016

Glad to hear it 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

`serializeArray()` not getting all fields - Stack Overflow
You haven't given the email and password inputs proper names, that's why the are excluded from the serialization. Give those input elements ......
Read more >
Ignoring empty form fields · Issue #28 - GitHub
As far as I could understand, there is no way to ignore empty form fields, excluding them from the resulting object.
Read more >
.serialize() | jQuery API Documentation
In this case, jQuery serializes the successful controls within the form. Only form elements are examined for inputs they contain, in all other...
Read more >
serialize - Manual - PHP
I was trying to submit a serialized array through a hidden form field using POST and was having a lot of trouble with...
Read more >
How to Convert HTML Form Field Values to a JSON Object
The FormData API doesn't directly convert form values to JSON, but we can get there by using the entries method and passing its...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found