Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 Reactive Forms FormControl errors is null

If I tab through the text inputs without entering anything, the error msg divs display indicating that the required validator has fired correctly. However, if I type anything into one of the fields, the console immediately throws this error:

Cannot read property 'required' of null

Here is my component:

import { PasswordValidators } from './../validators/password.validators';
import { Component, OnInit } from '@angular/core';
import { FormBuilder, Validators, FormGroup, FormControl } from '@angular/forms';

@Component({
  selector: 'app-changepassword-form',
  templateUrl: './changepassword-form.component.html',
  styleUrls: ['./changepassword-form.component.css']
})
export class ChangepasswordFormComponent {

  form;

  constructor(fb: FormBuilder) {
    this.form = fb.group({
      newPassword: ['', Validators.required],
      confirmPassword: ['', Validators.required]
    })
  }

  get newPassword() {
    return this.form.get('newPassword');
  }

  get confirmPassword() {
    return this.form.get('confirmPassword');
  }

}

and HTML:

<form [formGroup]="form">  
  <div class="form-group">
    <label for="newPassword">New Password</label>
    <input formControlName="newPassword" id="newPassword" type="text" class="form-control">
    <div class="alert alert-danger" *ngIf="newPassword.touched && newPassword.errors.required">
      Required
    </div>
  </div>
  <div class="form-group">
    <label for="confirmPassword">Confirm Password</label>
    <input formControlName="confirmPassword" id="confirmPassword" type="text" class="form-control">
    <div class="alert alert-danger" *ngIf="confirmPassword.touched && confirmPassword.errors.required">
      Required
    </div>
  </div>

  <p><button class="btn btn-primary">Submit</button></p>

</form>
like image 341
333Matt Avatar asked Aug 30 '17 00:08

333Matt


People also ask

What does FormControl Reset do?

reset()linkResets the form control, marking it pristine and untouched , and resetting the value. The new value will be the provided value (if passed), null , or the initial value if nonNullable was set in the constructor via FormControlOptions .

Is FormControl an observable?

Both FormControls and FormGroups expose an observable called valuesChanged . By subscribing to this observable we can react in real-time to changing values of an individual form control, or a group of form controls.


2 Answers

That error is coming from this:

*ngIf="newPassword.touched && newPassword.errors.required"

When you put in a value, there is no longer an errors collection so checking newPassword.errors.required generates that Cannot read property 'required' of null error.

Try something like this instead:

*ngIf="newPassword.touched && newPassword.errors?.required"

As an example, mine looks like this:

            <div class="col-md-8">
                <input class="form-control" 
                        id="productNameId" 
                        type="text" 
                        placeholder="Name (required)"
                        required
                        minlength="3"
                        [(ngModel)] = product.productName
                        name="productName"
                        #productNameVar="ngModel" />
                <span class="help-block" *ngIf="(productNameVar.touched ||
                                                 productNameVar.dirty || product.id !== 0) &&
                                                 productNameVar.errors">
                    <span *ngIf="productNameVar.errors.required">
                        Product name is required.
                    </span>
                    <span *ngIf="productNameVar.errors.minlength">
                        Product name must be at least three characters.
                    </span>
                </span>
            </div>
like image 122
DeborahK Avatar answered Sep 23 '22 08:09

DeborahK


You can also use the following syntax and it will work without the need to have a value first:

form.get('newPassword').hasError('required')

This will check for 'required' in the errors.

This will work as well and it's more concise :

form.hasError('required','newPassword')

If you are compiling with AOT option, according to this article, you should privilege hasError() syntax:

Don’t use control.errors?.someError, use control.hasError(‘someError’)

like image 41
Vega Avatar answered Sep 22 '22 08:09

Vega