Search code examples
angular-materialangular-reactive-formsangular-validationangular-custom-validatorsmat-error

Angular 11 Custom ISBN Validator Reactive Forms


I'm doing a custom validation for an ISBN number, I already have the function that checks the number and works perfect giving me the response by console, but I need to do the custom validator to get the error in the view with the form control and in this step this error is showing in console when I write an ISBN number, It's like it checks the errors but it doesn't know when its right and it should take the null response as a right ISBN number, at least thats what I saw in some examples.

core.js:6210 ERROR TypeError: Cannot read properties of null (reading 'CheckDigit')
at LibrosComponent_Template (libros.component.html:22)
at executeTemplate (core.js:9600)
at refreshView (core.js:9466)
at refreshComponent (core.js:10637)
at refreshChildComponents (core.js:9263)
at refreshView (core.js:9516)
at refreshEmbeddedViews (core.js:10591)
at refreshView (core.js:9490)
at refreshComponent (core.js:10637)
at refreshChildComponents (core.js:9263)

This is my typescript,

export class LibrosComponent implements OnInit {

 //ISBN Validator
 isbnValue: string = ""
 
 firstFormGroup: FormGroup;
 secondFormGroup: FormGroup;

  constructor(
   private _formBuilder: FormBuilder,
 ) {}

 ngOnInit() {
   this.firstFormGroup = this._formBuilder.group({
     tituloControl: ['', Validators.required],
     isbnControl: ['', Validators.required],
   },
   { validator: this.isbnValidate });    
 }
 
 isbnValidate(g: FormGroup) {
     var isbnValue = g.get('isbnControl').value
     var subject = isbnValue;

     // Checks for ISBN-10 or ISBN-13 format
   var regex = /^(?:ISBN(?:-1[03])?:? )?(?=[0-9X]{10}$|(?=(?:[0-9]+[- ]){3})[- 0-9X]{13}$|97[89][0-9]{10}$|(?=(?:[0-9]+[- ]){4})[- 0-9]{17}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9X]$/;

   if (regex.test(subject)) {
       // Remove non ISBN digits, then split into an array
       var chars = subject.replace(/[- ]|^ISBN(?:-1[03])?:?/g, "").split("");
       // Remove the final ISBN digit from `chars`, and assign it to `last`
       var last = chars.pop();
       var sum = 0;
       var check, i;

       if (chars.length == 9) {
           // Compute the ISBN-10 check digit
           chars.reverse();
           for (i = 0; i < chars.length; i++) {
               sum += (i + 2) * parseInt(chars[i], 10);
           }
           check = 11 - (sum % 11);
           if (check == 10) {
               check = "X";
           } else if (check == 11) {
               check = "0";
           }
       } else {
           // Compute the ISBN-13 check digit
           for (i = 0; i < chars.length; i++) {
               sum += (i % 2 * 2 + 1) * parseInt(chars[i], 10);
           }
           check = 10 - (sum % 10);
           if (check == 10) {
               check = "0";
           }
       }

       if (check != last) {
         return null;
           
       } else {
         return  g.get('isbnControl').setErrors( {CheckDigit: true} )
           
       }
     } else {
       return g.get('isbnControl').setErrors( {Invalid: true} );
   }

 }
}

In my HTML I have some inputs that are included in the form:

<form class="form" [formGroup]="firstFormGroup">  
          <div class="container-1">
            <mat-form-field class="width">
              <mat-label>Título</mat-label>
              <input matInput formControlName="tituloControl" required>
            </mat-form-field>

            <mat-form-field class="width">
                <mat-label>ISBN</mat-label>
                <input matInput formControlName="isbnControl" required>
                <mat-error *ngIf="firstFormGroup.controls['isbnControl'].pristine || firstFormGroup.controls.isbnControl.errors['CheckDigit']">Invalid ISBN check digit</mat-error>
                <mat-error *ngIf="firstFormGroup.controls['isbnControl'].pristine || firstFormGroup.controls.isbnControl.errors['Invalid']">Invalid ISBN</mat-error>
            </mat-form-field>
</form>


Solution

  • I already found the solution to the error, replacing the .errors [''] with hasError (''), the .errors[] is to read the property of the object that contains the validation error. But first I have to evaluate with the hasError () method if that property exists to access it.