Re: Submitting forms as JSON

Hi Brian,

On 25/02/2014 20:19 , Brian Kardell wrote:
> Would this also modify the FormData serialization in XMLHttpRequest[1].
>   That would be awesomely more useful to me.

As things stand today, when you construct a FormData from an 
HTMLFormElement, it forces the enctype to multipart/form-data (which I 
reckon is sensible).

So it does not automatically change FormData. But it would be relatively 
easy to make it do that.

>  It would also be wickedly
> handy if there were a way to get at it (ie, some DOM method), probably
> more useful than even the other two since it would let me polyfill the
> other two with 1 underlying implementation that explains it.
> <form>.toObjectModel() or something which provides a single canonical
> non-DOM object representation of the form which could be serialized and
> deserialized could be used to explain how enctype="application/json" and
> corresponding FormData in ajax would work too.

<form>.toObjectModel(), in order to fully match what browsers do, would 
have to be available only when submission is triggered. You could have a 
specific event on submission exposing this information (though I'm not 
sure that's all that convenient). Otherwise you're missing some of the 
information (though what you get may nevertheless be useful — that part 
is mostly polyfillable today).

>  Of course, this begs the
> question of whether there is a corresponding <form>.fromObjectModel()
> which could populate the form given some data (kinda awesome),

Populating a form without having to generate its values on the server 
would be awesome; the way this is currently done is one of the big warts 
of the platform. But I think that doing this right is harder.

> and
> whether both of those are maybe just natively handleable custom
> serializations ala JSON.stringify(form) and JSON.parse(form)..

Also note that if you want JSON out of an arbitrary form, the API needs 
to be asynchronous to account for files. That precludes just adding the 
usual toJSON() to HTMLFormElement.

(Though of course if TC-39 were to give us toJSON(cb) it could be sweet.)

-- 
Robin Berjon - http://berjon.com/ - @robinberjon

Received on Monday, 3 March 2014 14:20:21 UTC