0

I am new to Angular 2.

I have created a simple template which has two text field, I want to required field validate those two fields.

Login Form

<form #loginForm="ngForm" (ngSubmit)="onSubmit(loginForm)" novalidate>
    <div class="container">
        <div class="form-group">
            ooooo   <label><b>Username</b></label>
            <input type="text" placeholder="Enter Username" name="uname" required [(ngModel)]="UserData.uname" #uname="ngModel">

            <div *ngIf="loginForm.invalid"  class="alert alert-danger">
                <div [hidden]="!uname.errors.required"> Name is required </div>
            </div>
        </div>
        <div class="form-group">
            <label><b>Password</b></label>
            <input type="password" placeholder="Enter Password" name="pwd" required [(ngModel)]="UserData.pwd" #pwd="ngModel">
            <div *ngIf="UserData.pwd.errors && (UserData.pwd.dirty || UserData.pwd.touched)" class="alert alert-danger">
                <div [hidden]="!UserData.pwd.errors.required">Password is required </div>
            </div>
            <button type="submit"  >Login</button>
        </div>
    </div>
</form>

My Component

import { Component } from "@angular/core"
import { User } from "./UserModel"


@Component({
    selector: 'my-login',
    templateUrl:"app/Login/login.html"
})

export class LoginComponent
{
    //alert: any("hello");
    UserData: User = new User("", "");

    submitted = false;

    onSubmit(form: any) {
        alert("dfsdfsd" + form);
        if (!form.invalid) {
            alert(this.UserData.uname);
            alert(this.UserData.pwd);
            this.submitted = true;
        }
    }

}

What i want to implement is-

  1. When the form loads no validation message should appear?
  2. When user clicks on the submit button then the required message should appear?
  3. In both the textbox i have applied different type of checks to show the message that is inconsistent? so there should be a consistent way to solve this.

Many thanks for the help.

1 Answer 1

0

Maybe make use of the submitted variable, and use that in template, to not show message, until submitted is true, which we set it as in the submit function.

Also you wouldn't really need the two-way-binding here, since the object your form produces is directly assignable to your UserData.

The validation messages I'd just set then simply like this, where we are targeting the username:

<div *ngIf="uname.errors?.required && submitted"> Name is required </div>

in your submit function I'd pass loginForm.value as parameter instead of just loginForm. This way you get the form object ready to be used :)

And in your function you can assign the object to your UserData variable.

onSubmit(form: any) {
  this.submitted = true;
  this.UserData = form;
}

If you do want to keep the two-way-binding, it's of course totally possible! :)

DEMO

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.