Skip to content

New field: SubmitEvent.submittedFormData #242

@valtron

Description

@valtron

Introduction

When adding a submit handler to a form, one often wants to get the data that was submitted:

form.addEventListener('submit', e => {
	const data = new FormData(e.target);
});

But this contains a subtle bug: it's missing the data from the submitter element. The correct way to do it is using the submitter argument to FormData: new FormData(e.target, e.submitter).

It would be nice if, instead, a field with the submitted FormData already existed on SubmitEvent. This would simplify the manual, error-prone creation of FormData to e.submittedFormData.

As a bonus, this is easily shimmed.

Use Cases (Recommended)

Getting the submitted data from a submitted form.

Goals (Optional)

Add a new field to SubmitEvent that contains the submitted form data. The name doesn't matter, I just suggested submittedFormData arbitrarily, but it could be as simple as data or formData if that works.

Proposed Solution

Ibid.

Alternate Approaches (Optional)

This adds no new functionality, but makes code that gets submitted data cleaner and less error prone. The intention is obvious (e.submittedFormData == "get the submitted form data") rather than containing logic (manually creating a FormData, passing all the necessary arguments).

There is a related proposal (#207), but rather than adding a field on SubmitEvent, it adds a method sendWith that takes a callback which accepts a "submission" object that has the submitted data. The approach here is simpler for the stated use case.

Privacy & Security Considerations

No considerable privacy or security concerns are expected, but we welcome community feedback.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions