4

I am working on the form validation. I am using template-driven validation form whose coding is below. Its working fine now but when I try to add #username = "ngModel" and #password = "ngModel" in input to create validation classes it's showing me an error. Please find the error also.

<div class="container">
  <div class="row">
    <div class="centering text-center">
      <div class="login-cont col-md-4 col-md-offset-4 vcenter">
        <form id="login_form" name="login-form" #f="ngForm"  role="form" (ngSubmit)="f.form.valid && login()" novalidate>

          <input id="username" [(ngModel)]="username"  name="username"  required  class="form-control"  type="text"   placeholder="Username" >
<input id="userPassword" class="form-control"  required type="password" name="userPassword" required placeholder="Password" [(ngModel)]="password" >
<button type="submit" class="btn login-btn">Login</button>
</form>
</div>
</div>
</div>
</div>

 Cannot assign to a reference or variable!
    at _AstToIrVisitor.visitPropertyWrite (webpack-internal:///../../../compiler/esm5/compiler.js:26226:23)
    at PropertyWrite.visit (webpack-internal:///../../../compiler/esm5/compiler.js:4803:24)
    at convertActionBinding (webpack-internal:///../../../compiler/esm5/compiler.js:25676:45)
    at eval (webpack-internal:///../../../compiler/esm5/compiler.js:28166:22)
    at Array.forEach (<anonymous>)
    at ViewBuilder._createElementHandleEventFn (webpack-internal:///../../../compiler/esm5/compiler.js:28162:18)
    at nodes.(anonymous function) (webpack-internal:///../../../compiler/esm5/compiler.js:27581:27)
    at eval (webpack-internal:///../../../compiler/esm5/compiler.js:28107:22)
    at Array.map (<anonymous>)
    at ViewBuilder._createNodeExpressions (webpack-internal:///../../../compiler/esm5/compiler.js:28106:56)

This is what I tried but it gives me an error

   <input id="username" class="form-control"  type="text" required  name="username" placeholder="Username" [(ngModel)]="username" minlength="4"  #names="ngModel">


          <div *ngIf="names.invalid && (names.dirty || names.touched)"
               class="alert alert-danger">
          </div>
            <div *ngIf="names.errors.required">
              Name is required.
            </div>

            <div *ngIf="names.errors.minlength">
              Name must be at least 4 characters long.
            </div>

2 Answers 2

5

You are getting error because of your variable name and local variable name both are same

username is a variable that you are assigning to [(ngModel)]='username' , at the same time you are also trying to make local variable #username

Use different name like #username2 or #password2 , will solve your problem.

For more details read :

https://scotch.io/tutorials/using-angular-2s-template-driven-forms

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

6 Comments

so everything I should keep it same make #name = "ngModel" and use validation like <div *ngIf="name.errors.required"> Name is required. </div>
@Sahil, yes you can but never keep same name as varible, otherwise it will conflict.
it further gives me an error that it can read the property of undefined and undefined is name
Thanks tutorial helped me
@Sahil, Knew that will help you, :) , will you please also upvote the answer ?
|
0

FormBuilder can help you.

export class MyComp {
  myForm: FormGroup;
  constructor(private fb: FormBuilder) {
    this.myForm = fb.group({
      username: ['', [Validator.required]],
      userPassword: ['', [Validator.required]]
    });
  }
<div class="container">
  <div class="row">
    <div class="centering text-center">
      <div class="login-cont col-md-4 col-md-offset-4 vcenter">
        <form [formGroup]="myForm" id="login_form"(submit)="login()">
          <input id="username" class="form-control" type="text" placeholder="Username" >
          <input id="userPassword" class="form-control" type="password" placeholder="Password">
          <button type="submit" class="btn login-btn" [disabled]="myForm.invalid">Login</button>
        </form>
      </div>
    </div>
  </div>
</div>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.