jquery-serialize-object icon indicating copy to clipboard operation
jquery-serialize-object copied to clipboard

Symmetrical setter function "setJSON" would be nice

Open Ciantic opened this issue 12 years ago • 9 comments

Your plugin is exactly what I was looking for, but sometimes it's necessary to also set the form values from object, which would be reverse of this toJSON e.g. named form.setJSON(object).

Ideally toJSON output should be usable in setJSON directly, that is: form.setJSON(form.toJSON()) would work as expected, keeping values intact (though it should trigger changed events of course).

I know you haven't needed it, but I might implement it based on your toJSON solution if I can't find anything that does it. Will post my setJSON in public domain, and notify this issue when I find it.

Ciantic avatar Dec 09 '12 17:12 Ciantic

I know this is an old issue, but I've mostly left it unanswered as it's not really part of the responsibility of serializing form inputs to JSON.

I'm undecided on whether the functionality you're suggesting should be a completely separate plugin or bundled in with this plugin. I'm learning toward the former.

If the resulting complexity of the implementation is not too extreme, it might be well-suited in this plugin. But we won't know that until someone actually write it.

For now, this is a very low priority. In 1.5 years, no one else has chimed in requesting this functionality.

macek avatar Jul 10 '14 05:07 macek

We've received a second request for this.

A quick brainstorming of the solution reveals extremely complex implementation details. Setting the fields on basic static forms should not be too difficult, but dynamic forms would be particularly hard to work with.

At the time of serialization, there's a lot more information available; form controls have all sorts of state that isn't represented in the final serialized output. Reversing the entire serialization procedure might not even be possible in some cases.


Here's another thing that's coming to mind

<!-- array push style -->
<input name="foo[]" value="hello">
<input name="foo[]" value="world">

<!-- array explicit index -->
<input name="foo[0]" value="hello">
<input name="foo[1]" value="world">

<!-- new implicit push style (version 3.x) -->
<input name="foo" value="hello">
<input name="foo" value="world">

All of these forms produce the same result.

{"foo": ["hello", "world"]}

If we wanted to reverse this, we'd have a significant problem on our hands. In some cases, maybe even impossible

<!-- mixed, but still valid -->
<input name="foo[1]" value="a">
<input name="foo[]" value="b">
<input name="foo" value="c">

Result:

{"foo": [undefined, "a", "b", "c"]}

Using that json to somehow map each of those values back to the inputs seems nearly impossible without an extremely fragile and complex implementation.

macek avatar Nov 25 '14 19:11 macek

Personally I have not needed such a feature, but I do not doubt that it is useful in some applications.

I don't have the time available to implement this single-handedly. If someone wants to get the ball rolling on it, I'd be happy to help out in any way I'm capable of.

macek avatar Nov 25 '14 19:11 macek

I have implemented it a long time ago with tests. Maybe I should put it online, it does however work a bit differently at times.

Ciantic avatar Nov 25 '14 20:11 Ciantic

Hello again, here you go, I managed to dump it, I'm not sure if this is the "latest" since I use this on so many projects: https://github.com/Ciantic/jQuery.formJSON

Ciantic avatar Nov 25 '14 20:11 Ciantic

@Ciantic thanks for the reply. @spyropress I hope you find this helpful.

macek avatar Nov 25 '14 21:11 macek

was also looking for this feature too. A $("form").restoreForm(obj) method would be nice. Especially for working with micro js frameworks like RiotJS.

bchavez avatar Jun 28 '15 04:06 bchavez

@bchavez My https://github.com/Ciantic/jQuery.formJSON does have setter, and some rough tests. If you still need it.

I'm transitioning to react where this is rarely needed anymore, it also provides type safety for setting the form values, which means one can refactor and (TypeScript or Flow) compiler will yield errors if I forgot to rename something.

Ciantic avatar Jun 28 '15 08:06 Ciantic

I fully appreciate the lack of demand and difficulty (perhaps partial impossibility) of the task, but throwing a +1 in anyway

thanks for the great library! :-)

mrtristan avatar Apr 06 '16 14:04 mrtristan