2

I followed the angular 2 tutorial and tried to implement validation in a angular 2 form. See code:

<form #loginForm="ngForm" (ngSubmit)="login()">
    <div>
        <label for="email">Email:</label>
        <input type="text" id="email" name="email" required minlength="4" maxlength="24" [(ngModel)]="emailField" #name="ngModel" />
        <div *ngIf="email?.errors && (email?.dirty || email?.touched)" class="alert alert-danger">
            <div [hidden]="!email.errors.required">
              email is required
            </div>
            <div [hidden]="!email.errors.minlength">
              email must be at least 4 characters long.
            </div>
            <div [hidden]="!email.errors.maxlength">
              Name cannot be more than 24 characters long.
            </div>
        </div>
    </div>  
    <div>
        <label>Password:</label><input type="password" name="password" [(ngModel)]="passwordField" />
    </div>
    <div>
        <button [disabled]="!loginForm.form.valid" type="submit">Submit</button>
    </div>
</form>

The problem is that I am getting this error code :

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'errors' of undefined

I tried to implement it exactly as in the tutorial on the official site: https://angular.io/docs/ts/latest/cookbook/form-validation.html#!#template1

3
  • 3
    change #name="ngModel" to #email="ngModel" Commented Apr 7, 2017 at 13:03
  • 1
    Thank you George, that was it. Put an answer so I can upvote you and mark it as the correct answer. Commented Apr 7, 2017 at 13:04
  • 1
    No need to. I'm glad that the comment helped Commented Apr 7, 2017 at 14:22

1 Answer 1

2

The answer( thanks to George K ) was that I accidentally wrote #name="ngModel" instead of #email="ngModel"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.