1- import { Component , ElementRef , ViewChild } from '@angular/core' ;
21import { CommonModule } from '@angular/common' ;
2+ import { Component , ElementRef , ViewChild } from '@angular/core' ;
33import {
44 FormBuilder ,
55 ReactiveFormsModule ,
6- Validators ,
7- AbstractControl ,
8- ValidationErrors
6+ Validators
97} from '@angular/forms' ;
108import {
11- IonInput ,
129 IonButton ,
10+ IonContent ,
1311 IonHeader ,
14- IonToolbar ,
12+ IonInput ,
1513 IonTitle ,
16- IonContent ,
17- IonApp ,
18- IonButtons ,
19- IonItem ,
20- IonList
14+ IonToolbar
2115} from '@ionic/angular/standalone' ;
2216
23- // Custom validator for phone pattern
24- function phoneValidator ( control : AbstractControl ) : ValidationErrors | null {
25- const value = control . value ;
26- if ( ! value ) return null ;
27- const phonePattern = / ^ \( \d { 3 } \) \d { 3 } - \d { 4 } $ / ;
28- return phonePattern . test ( value ) ? null : { invalidPhone : true } ;
29- }
30-
3117@Component ( {
3218 selector : 'app-input-validation' ,
3319 templateUrl : './input-validation.component.html' ,
@@ -36,16 +22,12 @@ function phoneValidator(control: AbstractControl): ValidationErrors | null {
3622 imports : [
3723 CommonModule ,
3824 ReactiveFormsModule ,
39- IonApp ,
4025 IonInput ,
4126 IonButton ,
4227 IonHeader ,
4328 IonToolbar ,
4429 IonTitle ,
45- IonContent ,
46- IonButtons ,
47- IonItem ,
48- IonList
30+ IonContent
4931 ]
5032} )
5133export class InputValidationComponent {
@@ -125,11 +107,11 @@ export class InputValidationComponent {
125107 onIonBlur ( fieldName : string , inputElement : IonInput ) : void {
126108 this . markTouched ( fieldName ) ;
127109 this . updateValidationClasses ( fieldName , inputElement ) ;
128-
110+
129111 // Update aria-live region if invalid
130112 if ( this . isInvalid ( fieldName ) && this . debugRegion ) {
131113 const metadata = this . fieldMetadata [ fieldName as keyof typeof this . fieldMetadata ] ;
132- this . debugRegion . nativeElement . textContent =
114+ this . debugRegion . nativeElement . textContent =
133115 `Field ${ metadata . label } is invalid: ${ metadata . errorText } ` ;
134116 console . log ( 'Field marked invalid:' , metadata . label , metadata . errorText ) ;
135117 }
@@ -152,12 +134,19 @@ export class InputValidationComponent {
152134
153135 // Update validation classes on the input element
154136 private updateValidationClasses ( fieldName : string , inputElement : IonInput ) : void {
155- const element = inputElement as any ;
156-
137+ // Access the native element through the Angular component
138+ const element = ( inputElement as any ) . el || ( inputElement as any ) . nativeElement ;
139+
140+ // Ensure we have a valid element with classList
141+ if ( ! element || ! element . classList ) {
142+ console . warn ( 'Could not access native element for validation classes' ) ;
143+ return ;
144+ }
145+
157146 if ( this . isTouched ( fieldName ) ) {
158147 // Add ion-touched class
159148 element . classList . add ( 'ion-touched' ) ;
160-
149+
161150 // Update ion-valid/ion-invalid classes
162151 if ( this . isInvalid ( fieldName ) ) {
163152 element . classList . remove ( 'ion-valid' ) ;
@@ -189,16 +178,16 @@ export class InputValidationComponent {
189178 onReset ( ) : void {
190179 // Reset form values
191180 this . form . reset ( ) ;
192-
181+
193182 // Clear touched fields
194183 this . touchedFields . clear ( ) ;
195-
184+
196185 // Remove validation classes from all inputs
197186 const inputs = document . querySelectorAll ( 'ion-input' ) ;
198187 inputs . forEach ( input => {
199188 input . classList . remove ( 'ion-valid' , 'ion-invalid' , 'ion-touched' ) ;
200189 } ) ;
201-
190+
202191 // Clear aria-live region
203192 if ( this . debugRegion ) {
204193 this . debugRegion . nativeElement . textContent = '' ;
0 commit comments