Skip to content

Commit f04fa23

Browse files
authored
test(angular): validate checkbox and toggle in lazy template-form (#31005)
## What is the current behavior? Checkbox and toggle components are not validated to be ticked/on in `packages/angular/test/base/src/app/lazy/template-form`. This prevents the error text from being displayed. While they have the `required` attribute, this only applies to accessibility for [checkbox](https://ionicframework.com/docs/api/checkbox#required) and [toggle](https://ionicframework.com/docs/api/toggle#required). ## What is the new behavior? - Use an Angular validator directive for checkbox and toggle. - Make template-form an Angular module so the validator directive can be imported. ## Does this introduce a breaking change? - [ ] Yes - [X] No
1 parent ce83407 commit f04fa23

File tree

7 files changed

+82
-5
lines changed

7 files changed

+82
-5
lines changed

packages/angular/test/base/src/app/lazy/app-lazy/app.module.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import { AlertComponent } from '../alert/alert.component';
2828
import { AccordionComponent } from '../accordion/accordion.component';
2929
import { AccordionModalComponent } from '../accordion/accordion-modal/accordion-modal.component';
3030
import { TabsBasicComponent } from '../tabs-basic/tabs-basic.component';
31-
import { TemplateFormComponent } from '../template-form/template-form.component';
3231

3332
@NgModule({
3433
declarations: [
@@ -54,8 +53,7 @@ import { TemplateFormComponent } from '../template-form/template-form.component'
5453
AlertComponent,
5554
AccordionComponent,
5655
AccordionModalComponent,
57-
TabsBasicComponent,
58-
TemplateFormComponent
56+
TabsBasicComponent
5957
],
6058
imports: [
6159
CommonModule,

packages/angular/test/base/src/app/lazy/app-lazy/app.routes.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { NavigationPage3Component } from '../navigation-page3/navigation-page3.c
1919
import { AlertComponent } from '../alert/alert.component';
2020
import { AccordionComponent } from '../accordion/accordion.component';
2121
import { TabsBasicComponent } from '../tabs-basic/tabs-basic.component';
22-
import { TemplateFormComponent } from '../template-form/template-form.component';
2322

2423
export const routes: Routes = [
2524
{
@@ -34,7 +33,7 @@ export const routes: Routes = [
3433
{ path: 'textarea', loadChildren: () => import('../textarea/textarea.module').then(m => m.TextareaModule) },
3534
{ path: 'searchbar', loadChildren: () => import('../searchbar/searchbar.module').then(m => m.SearchbarModule) },
3635
{ path: 'form', component: FormComponent },
37-
{ path: 'template-form', component: TemplateFormComponent },
36+
{ path: 'template-form', loadChildren: () => import('../template-form/template-form.module').then(m => m.TemplateFormModule) },
3837
{ path: 'modals', component: ModalComponent },
3938
{ path: 'modal-inline', loadChildren: () => import('../modal-inline').then(m => m.ModalInlineModule) },
4039
{ path: 'modal-sheet-inline', loadChildren: () => import('../modal-sheet-inline').then(m => m.ModalSheetInlineModule) },
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { NgModule } from "@angular/core";
2+
import { RouterModule } from "@angular/router";
3+
import { TemplateFormComponent } from "./template-form.component";
4+
5+
@NgModule({
6+
imports: [
7+
RouterModule.forChild([
8+
{
9+
path: '',
10+
component: TemplateFormComponent
11+
}
12+
])
13+
],
14+
exports: [RouterModule]
15+
})
16+
export class TemplateFormRoutingModule { }

packages/angular/test/base/src/app/lazy/template-form/template-form.component.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
[(ngModel)]="checkboxValue"
110110
name="checkboxField"
111111
required
112+
requireTrue
112113
#checkboxField="ngModel"
113114
id="template-checkbox-test"
114115
helper-text="You must agree to continue"
@@ -133,6 +134,7 @@
133134
[(ngModel)]="toggleValue"
134135
name="toggleField"
135136
required
137+
requireTrue
136138
#toggleField="ngModel"
137139
id="template-toggle-test"
138140
helper-text="You must turn on to continue"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { CommonModule } from "@angular/common";
2+
import { NgModule } from "@angular/core";
3+
import { FormsModule, ReactiveFormsModule } from "@angular/forms";
4+
import { IonicModule } from "@ionic/angular";
5+
6+
import { TemplateFormRoutingModule } from "./template-form-routing.module";
7+
import { TemplateFormComponent } from "./template-form.component";
8+
import { ValidatorsModule } from "../validators/validators.module";
9+
10+
@NgModule({
11+
imports: [
12+
CommonModule,
13+
FormsModule,
14+
ReactiveFormsModule,
15+
IonicModule,
16+
TemplateFormRoutingModule,
17+
ValidatorsModule
18+
],
19+
declarations: [
20+
TemplateFormComponent
21+
]
22+
})
23+
export class TemplateFormModule { }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Directive, forwardRef } from '@angular/core';
2+
import { NG_VALIDATORS, Validator, AbstractControl, ValidationErrors, Validators } from '@angular/forms';
3+
4+
@Directive({
5+
selector: '[requireTrue]',
6+
providers: [
7+
{
8+
provide: NG_VALIDATORS,
9+
useExisting: forwardRef(() => RequireTrueValidatorDirective),
10+
multi: true,
11+
},
12+
],
13+
standalone: false,
14+
})
15+
export class RequireTrueValidatorDirective implements Validator {
16+
validate(control: AbstractControl): ValidationErrors | null {
17+
return Validators.requiredTrue(control);
18+
}
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { CommonModule } from "@angular/common";
2+
import { NgModule } from "@angular/core";
3+
import { FormsModule, ReactiveFormsModule } from "@angular/forms";
4+
5+
import { RequireTrueValidatorDirective } from "./require-true.directive";
6+
7+
@NgModule({
8+
imports: [
9+
CommonModule,
10+
FormsModule,
11+
ReactiveFormsModule
12+
],
13+
declarations: [
14+
RequireTrueValidatorDirective
15+
],
16+
exports: [
17+
RequireTrueValidatorDirective
18+
]
19+
})
20+
export class ValidatorsModule { }

0 commit comments

Comments
 (0)