Asynchronous custom validation throws an error for the console: "Unable to read property 'required' from null in Object.eval [as updateDirectives]"

I am currently working on defining reactive forms with Maximilian Schwarzmüller's Angular 4 tutorial. In an assignment, I had to create a reactive form, and I did it. Then I had to create my own asynchronous validator that validates the value of the control. It doesn't have to equal "Test". This is my typescript:

import {Component, OnInit} from '@angular/core';
import {FormControl, FormGroup, Validators} from '@angular/forms';

import {Observable} from 'rxjs/Observable';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  statuses = ['Stable', 'Critical', 'Finished'];
  signupForm: FormGroup;

  ngOnInit() {
    this.signupForm = new FormGroup({
      'projectName': new FormControl(null, [Validators.required], this.forbiddenName),
      'email': new FormControl(null, [Validators.required, Validators.email]),
      'projectStatus': new FormControl('Stable')
    });
  }

  onSubmit() {
    console.log(this.signupForm.value);
    console.log(this.signupForm);
  }

  forbiddenName(control: FormControl): Promise<any> | Observable<any> {
    const promise = new Promise<any>((resolve, reject) => {
      setTimeout(() => {
        if (control.value === 'Test') {
          resolve({'projectNameIsForbidden': true});
        } else {
          resolve(null);
        }
      }, 2000);
    });
    return promise;
  }

}

      

And here is my HTML:

<div class="container">
  <div class="row">
    <div class="col-xs-12 col-sm-10 col-md-8 col-sm-offset-1 col-md-offset-2">
      <form [formGroup]="signupForm" (ngSubmit)="onSubmit()">
        <div class="form-group">
          <label for="project-name">Project name</label>
          <input type="text" id="project-name" class="form-control" formControlName="projectName">
          <div class="help-block" *ngIf="!signupForm.get('projectName').valid && signupForm.get('projectName').touched">
            <span *ngIf="signupForm.get('projectName').errors['required']">Can't be empty!<br></span>

            <span *ngIf="signupForm.get('projectName').errors['projectNameIsForbidden']">This name is forbidden!</span>
          </div>
        </div>
        <div class="form-group">
          <label for="email">Email</label>
          <input type="email" id="email" class="form-control" formControlName="email">
          <div class="help-block" *ngIf="!signupForm.get('email').valid && signupForm.get('email').touched">
            <span *ngIf="signupForm.get('email').errors['required']">Can't be blank!<br></span>
            <span *ngIf="signupForm.get('email').errors['email']">Has invalid format!</span>
          </div>
        </div>
        <div class="form-group">
          <label for="project-status">Project Status</label>
          <select id="project-status" class="form-control" formControlName="projectStatus">
            <option *ngFor="let status of statuses">{{ status }}</option>
          </select>
        </div>
        <button class="btn btn-success" type="submit">Submit</button>
      </form>
    </div>
  </div>
</div>

      

It seems to work fine, it gives me the appropriate error messages in my opinion, but in the console I get an error every time I click on projectName

the control projectName

. This is mistake:

enter image description here

So why does the error appear? Thanks in advance.

+8


source to share


2 answers


The reason for the error is here:

<span *ngIf="signupForm.get('projectName').errors['required']">
   Can't be empty!
</span>

      

While you are typing errors

it becomes null

, and if you step out of the field before the asynchronous validation checker evaluates, there errors

will be null

, so Angular won't be able to read it. This can be solved with a safe navigator:



<span *ngIf="signupForm.get('projectName').errors?.required">

      

But, since I prefer to show messages, is used hasError

, so I would change these checks instead:

<span *ngIf="signupForm.hasError('projectNameIsForbidden', 'projectName')">
<span *ngIf="signupForm.hasError('required', 'projectName')">

      

+34


source




 <mat-form-field>
      <input matInput placeholder="password" minlength="4" name="password" [(ngModel)]="loginInput.password" #password="ngModel"
        type="password" required>
      <mat-error *ngIf="username?.invalid">password is required</mat-error>
    </mat-form-field> 
    
    <!-- Use ? sign after input name.-->
      

Run codeHide result


0


source







All Articles