Form Accessors
The directives that connect <input> and <textarea> to Angular forms: Signal Forms' [formField], reactive forms and ngModel. import { LYNX_FORM_ACCESSORS, LynxInputValueAccessor, LynxTextareaValueAccessor, type LynxFieldValue, type LynxFieldParseError } from '@blotch/angular-lynx';
Usage
LYNX_ELEMENTS already contains both accessors. Bind a form directive, and the accessor switches on:
- Angular forms calls their members. You don't.
- A
type="number"ortype="digit"field gives a number, ornullwhile it's empty, unless the form gave it a string. - Text that isn't a number leaves the value as it was and puts a
parseerror on the field. See Numbers. - A form's
maxLengthsets the field'smaxlength. See Constraints.
API
LYNX_FORM_ACCESSORS
Both form value accessors as one array. LYNX_ELEMENTS already contains them.
LynxInputValueAccessor and LynxTextareaValueAccessor
Connect <input> and <textarea> to Signal Forms' [formField], reactive forms and ngModel.
- They switch on only when one of those form directives is on the element.
- Both have the members of
LynxFieldValueAccessor, below.
onInput()
Runs on bindinput. Sends the typed text, event.detail.value, to the form control: as a number in a number field.
onBlur()
Runs on bindblur. Marks the control as touched.
writeValue()
Shows the form value in the field, as a [value] binding does: from the field's first render, and after form.reset() and setValue().
nullandNaNshow as an empty field.- On iOS and Android a Lynx field has no
valueattribute. So the element sets its text with thesetValueUI method instead.
registerOnChange() and registerOnTouched()
Store the callbacks Angular forms passes in.
setDisabledState()
Sets or removes the element's disabled attribute.
validate() and registerOnValidatorChange()
validate() returns { parse: { text } } while a number field's text isn't a number, and null otherwise.
- Signal Forms reads it as the field's
parseerror. - Reactive and template-driven forms read it as one more validator.
Types
LynxFieldValue and LynxFieldParseError
What a field gives its form, and the context of its parse error.
ControlValueAccessor, Validator and ValidationErrors come from @angular/forms.
Related
- Forms: Signal Forms, reactive forms and
ngModelon Lynx. - Elements:
LYNX_ELEMENTS <input>and<textarea>
