with one click
canopy-forms-validation
// Best practices for Canopy form validation. Trigger when displaying inline form errors, setting up error state matching, or implementing form-level validation in an Angular project using Canopy.
// Best practices for Canopy form validation. Trigger when displaying inline form errors, setting up error state matching, or implementing form-level validation in an Angular project using Canopy.
[HINT] Download the complete skill directory including SKILL.md and all related files
| name | canopy-forms-validation |
| description | Best practices for Canopy form validation. Trigger when displaying inline form errors, setting up error state matching, or implementing form-level validation in an Angular project using Canopy. |
| license | MIT |
| metadata | {"source":"https://github.com/Legal-and-General/canopy/tree/master/projects/canopy/src/lib/forms/validation/docs/guide.mdx"} |
This skill provides usage guidance for form validation with LgValidationComponent and LgErrorStateMatcher from @legal-and-general/canopy.
Apply this skill whenever you display inline form errors in a Canopy form.
import {
LgValidationComponent,
LgErrorStateMatcher,
} from '@legal-and-general/canopy';
Inject LgErrorStateMatcher and use it to determine when to show errors:
constructor(
private fb: FormBuilder,
private errorState: LgErrorStateMatcher,
) {
this.form = this.fb.group({
username: ['', [Validators.required, Validators.minLength(4)]],
});
}
isControlInvalid(control: AbstractControl, form: FormGroupDirective): boolean {
return this.errorState.isControlInvalid(control, form);
}
Always add #userForm="ngForm" to the <form> element and ensure the submit button is inside the form.
<form [formGroup]="form" (ngSubmit)="onSubmit(form)" #userForm="ngForm">
<lg-input-field>
Username
@if (isControlInvalid(form.get('username'), userForm) && form.get('username').hasError('required')) {
<lg-validation>Username is required</lg-validation>
}
@if (isControlInvalid(form.get('username'), userForm) && form.get('username').hasError('minlength')) {
<lg-validation>Username must be at least 4 characters</lg-validation>
}
<input lgInput formControlName="username" />
</lg-input-field>
<button type="submit">Submit</button>
</form>
| Input | Type | Default | Description |
|---|---|---|---|
status | 'generic' | 'info' | 'warning' | 'error' | 'success' | 'error' | Visual and semantic status. |
showIcon | boolean | true | Whether to show the status icon. |
Validation that fires while the user types is permitted only in specific contexts (e.g. password fields).