ElementInternals Form-Associated Custom Element
Let a custom element join form validation by attaching ElementInternals: set the submitted value and drive checkValidity with setValidity.
Code
if (typeof document === "undefined") {
console.log("rating-stars needs a browser DOM to register the custom element");
} else {
// Form-associated custom element: the element participates in form
// submission and constraint validation through ElementInternals
class RatingStars extends HTMLElement {
static formAssociated = true;
readonly internals: ElementInternals;
constructor() {
super();
this.internals = this.attachInternals();
}
setRating(value: number): void {
this.internals.setFormValue(value === 0 ? null : String(value));
if (value === 0) {
this.internals.setValidity({ valueMissing: true }, "Pick at least one star");
} else {
this.internals.setValidity({});
}
}
}
customElements.define("rating-stars", RatingStars);
const rating = document.createElement("rating-stars") as RatingStars;
document.body.append(rating);
rating.setRating(0);
console.log("valid:", rating.internals.checkValidity(), "| message:", rating.internals.validationMessage);
rating.setRating(4);
console.log("valid:", rating.internals.checkValidity(), "| message:", JSON.stringify(rating.internals.validationMessage));
}Output
Captured from a sandboxed run of this snippet in the build pipeline.
rating-stars needs a browser DOM to register the custom elementWalking through the code
The snippet is organized around the class RatingStars.
What to watch for
- Self-contained: imports go no further than what the runtime provides.
- A browser is the runtime here:
documentare not available in Node.
Using it
The demo output is labeled (valid:), so the output lines each link back to the operation behind them.
Key statements
this.internals.setValidity({ valueMissing: true }, "Pick at least one star");
const rating = document.createElement("rating-stars") as RatingStars;
this.internals.setFormValue(value === 0 ? null : String(value));
Interactive Tools for This Topic
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.Credit Card Brand DetectorIdentify the card network from a number's prefix and length, validate the Luhn checksum, and show a masked form and the issuer identification number.
FAQ
- Does ElementInternals Form-Associated Custom Element run in plain Node.js?
- No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.