Form Controls
Glint types a <form>'s elements from the named controls in its template. A submit handler declares the controls it reads, and Glint checks that declaration against the template.
import Component from '@glimmer/component';
import { on } from '@ember/modifier';
export default class NewProject extends Component {
submit = (
event: SubmitEvent & {
currentTarget: HTMLFormElement & {
elements: {
name: HTMLInputElement;
plan: HTMLSelectElement;
public: HTMLInputElement;
size: RadioNodeList & { value: 'small' | 'large' | '' };
};
};
},
) => {
event.preventDefault();
let { name, plan, public: isPublic, size } = event.currentTarget.elements;
save({ name: name.value, plan: plan.value, isPublic: isPublic.checked, size: size.value });
};
<template>
<form {{on "submit" this.submit}}>
<input name="name" required />
<select name="plan">…</select>
<input name="public" type="checkbox" />
<input name="size" type="radio" value="small" />
<input name="size" type="radio" value="large" />
</form>
</template>
}No as HTMLInputElement, no formData.get('name') as string.
What it catches
The error is reported on the form's {{on "submit"}}.
name="plan" renamed or removed
plan: HTMLSelectElement
'plan' is missing
<select name="plan"> changed to <input>
plan: HTMLSelectElement
type mismatch
{{#if @pro}}<select name="plan">{{/if}}
plan: HTMLSelectElement
must be plan?:
a second name="plan", or another control id="plan"
plan: HTMLSelectElement
it is a RadioNodeList
<input type="radio" name="size" value="medium">
value: 'small' | 'large' | ''
'medium' not assignable
How controls are found
Controls: static
nameoridonbutton,fieldset,input,object,output,selectandtextarea, the HTML spec's listed elements.<input type="image">is left out, becauseelementsdoes not include it.{{#if}}/{{#unless}}: optional, unless every branch renders the control.{{#let}}: no change.{{#each}}, component blocks: optional, and may be aRadioNodeList. An{{#each}}whose{{else}}renders the control too makes it required.A dynamic
type: optional if it can beimage.{{if}}and{{unless}}with string branches are read.Radio groups with static
values:valueis the union of them, or''while none is checked.unknown: a name on a component, which may render any number of controls under it, or none, and on a custom element, which may be form-associated....attributeson a control: optional, because the caller can replace itsname.form="…": a control that names another form'sid, orform="", is left out. One that names this form'sid, or a dynamic one, is optional, because another copy of the form can have the sameid.Not seen: dynamic names and
ids, names andids a caller passes through...attributes,{{#in-element}}, controls that a component,{{yield}},{{outlet}},{{mount}}or{{{html}}}renders, and controls outside the form that join it withform="…". If one of them has the same name as a control in the template,elementsreturns aRadioNodeListthat the type does not show. If one of them is namedelements, it replacesform.elements.Not typed: a form with a control, image, component or custom element named
elements, which replacesform.elements, and forms in.gjsfiles.
Limits
Optional controls are not checked. TypeScript does not report a declared optional property that the template lacks, so
{ email?: HTMLInputElement }still compiles aftername="email"is renamed.Without
strictFunctionTypes, the handler's parameter is compared in both directions. A removed or conditional control, a new radio value, or a name on a component is then not reported.
Last updated