-
-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deserialize FormData #137
Comments
Setting Thus: function hydrateForm(formData, formElement) {
for (const [name, value] of formData.entries()) {
const escapedName = CSS.escape(name);
const input = formElement.querySelector(`[name='${escapedName}']`);
if (input === null) {
throw new Error(name === escapedName ? `Form element for ${name} not found` : `Form element for ${name} (escaped: ${escapedName}) not found`);
}
input.value = value;
}
} |
"Setting the value" of a checkbox means:
If you just set |
Ok that reveals a slight confict: both of these are saved as the same thing; <input type="checkbox" name="state" checked> <input type="checkbox" name="state" value="on" checked>
<input type="checkbox" name="state" value="off"> But that seems pretty minor, though worth documenting if it ever gets published. What's the use case, btw? |
No, that’s the standard behavior of FormData. Unchecked fields are not included in the data. So "unchecked === might as well not exist." The only thing your example shows is that it will need to match the data FormData doesn’t save the exact HTML, but just the data. This is the main difference with “classic” serializers which would have a hard time picking the right value for those fields (because they usually save |
I use the mentioned module in https://github.com/fregante/webext-options-sync |
There's an excellent module for serializing and deserializing forms: https://www.npmjs.com/package/dom-form-serializer
However this is quite old and we have since gained FormData as a way to serialize forms into a string, which
dom-form-serializer
doesn't support (it has its own object format)What's missing is a way to apply the contents of FormData back to a form (rehydrate, if you will).
This
form.elements.namedItem()
API can help match the field name in FormData to the actual fields, but then the logic to select the right checkbox, radio, select, multi-select isn't straightforward. It'd be great to have a modern, simple way to do this.The text was updated successfully, but these errors were encountered: