liveBook · Manning

formAssociated

As explained in Web Component Development with Modern Libraries and Tooling

A custom-element capability that allows an element to participate in form behavior and submission.

A custom element inside a form does not contribute data to submission by default. Set `static formAssociated = true` on its class to opt in, then use `ElementInternals` to provide the value the form should submit. section 3.9

A radio group in a form

A `<radio-group name="primaryColor" values="red,green,blue">` can sit beside Submit and Reset buttons. Its `name` supplies the form-data key; its selected color supplies the value. section 3.10 section 3.9

For a vanilla custom element:

  • Declare `static formAssociated = true` on the class, not in its constructor or an instance method. Obtain internals with `this.attachInternals()`. section 3.9
  • Call `internals.setFormValue(value)` for the initial value and whenever it changes. Pass a `FormData` object instead to submit multiple named values. section 3.9
  • For reset behavior, save the initial value in `formAssociatedCallback()` and restore it in `formResetCallback()`. section 3.10

With Stencil, set `formAssociated: true` in `@Component` and obtain internals through `@AttachInternals()`. Call `setFormValue` during `componentWillLoad` and after value changes. A single submitted value uses the element’s `name` as its key; with `FormData`, the component must choose the keys. section 6.12 section 3.9

Listing 3.14 radio-group example
<form action="https://httpbin.org/post" method="post"> #1
  <radio-group
    legend="Primary Color"
    labels="Red,Green,Blue"
    name="primaryColor"
    value="red"
    values="red,green,blue"
  >
    <div slot="before">Choose a primary color.</div> #2
    <div slot="after">This will be the most used color.</div> #3
  </radio-group>
  <div>
    <button type="submit">Submit</button>
    <button type="reset">Reset</button>
  </div>
</form>

The complete form markup shows the radio group’s `name` and initial `value` alongside submit and reset controls. section 3.10

How Web Component Development with Modern Libraries and Tooling covers it

How deep it goes
Walks through class opt-in, ElementInternals, initial and changed values, multiple values, and reset callbacks.
Examples
Uses a radio-group form with submit and reset buttons, plus single-value and multi-value submission code.
What it assumes
Assumes basic HTML form and custom-element familiarity while teaching the ElementInternals submission steps explicitly.
What you can do after
Build it from scratch in vanilla JavaScript or apply it via Stencil to submit a custom element’s values.
How it connects
Connects custom elements to HTML forms, ElementInternals, FormData, lifecycle callbacks, and Stencil decorators.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Component Development with Modern Libraries and Tooling. The section links point to the parts of the book it draws on.