forms
Helpers for serializing, resetting, and populating HTML forms, plus native validation and submit handling.
Import: @rtorcato/browser-common/forms
๐ MDN: HTMLFormElement
Browser supportโ
| API | Chrome | Edge | Firefox | Safari | iOS Safari | Baseline |
|---|---|---|---|---|---|---|
FormData | โ 5+ | โ 12+ | โ 4+ [1] | โ 5+ | โ 5+ | Widely available |
HTMLFormElement.reportValidity | โ 40+ | โ 17+ | โ 49+ | โ 10.1+ | โ 10.3+ | Widely available |
- Firefox: Before Firefox 7, specifying a Blob as the data to append to the object, the filename reported in the Content-Disposition HTTP header was an empty string, resulting in errors on some servers. Starting with Firefox 7, the filename blob is sent.
โ
supported ยท โ partial ยท โ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: FormData ยท HTMLFormElement.reportValidity
Exampleโ
import { serializeForm, isFormValid, onFormSubmit } from '@rtorcato/browser-common/forms'
const off = onFormSubmit(myForm, (event) => {
if (!isFormValid(myForm)) return
const data = serializeForm(myForm)
console.log(data)
})
off()
Exportsโ
serializeForm(form)โ returns form field values asRecord<string, string>resetForm(form)โ resets the form to its initial valuessetFormValues(form, values)โ writes values onto matching named fieldsisFormValid(form)โ runs the browser's built-incheckValidity()onFormSubmit(form, callback)โ listens for submit, callspreventDefault()first; returns an unsubscribe function
See the API reference for full signatures.