- Docs
- Restore defaults with reset()
Restore defaults with reset()
Restores the native form, clears manual and validation errors, resynchronizes visual state, re-emits field watchers and notifies subscribers.
Restores the form to its initial state: native field values reset, manual and validation errors cleared, visual attributes synchronized, subscribers notified.
Signature
reset(): FormController
Returns
The same FormController — chainable.
Behaviour
When invoked, the controller:
- Calls
state.element.reset()(the native form reset). - Clears
state.manualErrorsandstate.validationErrors. - Restores matching visible/raw defaults for formatted fields.
- Removes every
css-filled,css-error, andaria-invalidattribute from associated fields. - Re-syncs visual state. Controls whose native default value is non-empty regain
css-filled, even whenautoValidateisfalse; errors andaria-invalidstay cleared. - Calls every
watchandobservecallback with the restored value, so counters and previews follow the reset (since 2.8.0). - Notifies subscribers.
A native <button type="reset"> or a direct form.reset() call runs the same state pass after the browser restores the defaults, unless a reset listener cancels the event (since 2.8.0).
Examples
Reset after a successful submit
import { form } from '@samline/forms'
const profile = form('profile-form')
profile.onSubmit(async (_element, _data, formData) => {
await fetch('/profile', { method: 'POST', body: formData })
profile.reset()
})
Reset on cancel
const signup = form('signup-form')
document.querySelector('#cancel')?.addEventListener('click', () => {
signup.reset()
})
Edge cases
- Native
reset()does not dispatchinputevents for fields whose value changes because of the reset. The controller restores formatted mirrors, visual/ARIA state and watchers explicitly; it does not run validation or auto-submit. - A destroyed controller ignores
reset(). - Submit handlers are not invoked.
reset()is purely a UI / state operation. - The
submitCountfromgetState()is not reset. It tracks the lifetime of the controller, not the lifetime of a single form session. - Pending async submit work is not cancelled.
isSubmittingremains true until the outstandingonSubmitpromises settle.
Related
destroy— full teardown.clearErrors— clear errors without resetting field values.