Reputation: 7444
I am writing simple login form, by trying to combine these two tutorials:
But I am stuck. Validation doesn't work as intended. I made a workaround by sending "message" (i.e. value, when I inspect loginForm.value) from component to template, but that is not how it should work. Where did I go wrong with this?
Here is part of the code I can't make work:
login.component.html
<div class="container">
<div class="col-md-6">
<h2>Log In</h2>
<hr>
<p *ngIf="message" class="text-center error">{{message}}</p>
<form [formGroup]="loginForm" (ngSubmit)="onSubmit()">
<div class="form-group clearfix" [ngClass]="{ 'has-error': submitted && f.userid.errors }">
<label for="userid" class="col-md-3 col-sm-5 col-xs-12">Userid</label>
<div class="col-md-9 col-sm-7 col-xs-12">
<input type="text" class="form-control" formControlName="userid" />
<div *ngIf="submitted && f.userid.errors" class="help-block">
<div *ngIf="f.userid.errors.required">Userid is required</div>
</div>
</div>
</div>
<div class="form-group clearfix" [ngClass]="{ 'has-error': submitted && f.password.errors }">
<label for="password" class="col-md-3 col-sm-5 col-xs-12">Password</label>
<div class="col-md-9 col-sm-7 col-xs-12">
<input type="password" class="form-control" formControlName="password" />
<div *ngIf="submitted && f.password.errors" class="help-block">
<div *ngIf="f.password.errors.required">Password is required</div>
</div>
</div>
</div>
<div class="col-xs-12 form-group text-right">
<button class="btn btn-success" type="submit">Login</button>
</div>
</form>
</div>
</div>
login.component.ts
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { Login } from '../login';
import { AuthService } from '../auth.service';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
model: Login = { userid: "admin", password: "nenad123" };
loginForm: FormGroup;
message: string;
returnUrl: string;
constructor(private formBuilder: FormBuilder, private router: Router, public authService: AuthService) { }
ngOnInit() {
this.loginForm = this.formBuilder.group({
userid: ['', Validators.required],
password: ['', Validators.required]
});
this.returnUrl = '/home';
this.authService.logout();
}
get f() { return this.loginForm.controls; }
onSubmit() {
if (this.loginForm.invalid) {
let user = this.loginForm.value.userid;
let pass = this.loginForm.value.password;
if (user == "" && pass == "") { this.message = "UserId and Password cannot be empty"; }
else if (user == "") { this.message = "UserId cannot be empty"; }
else if (pass == "") { this.message = "Password cannot be empty"; }
else { this.message = "Please check your userid and password"; }
return;
}
else {
if (this.f.userid.value == this.model.userid && this.f.password.value == this.model.password) {
localStorage.setItem('isLoggedIn', "true");
localStorage.setItem('token', this.f.userid.value);
this.router.navigate([this.returnUrl]);
}
else {
this.message = "Please check your userid and password";
}
}
}
}
This part is temporary workaround:
if (this.loginForm.invalid) {
let user = this.loginForm.value.userid;
let pass = this.loginForm.value.password;
if (user == "" && pass == "") { this.message = "UserId and Password cannot be empty"; }
else if (user == "") { this.message = "UserId cannot be empty"; }
else if (pass == "") { this.message = "Password cannot be empty"; }
else { this.message = "Please check your userid and password"; }
return;
}
Upvotes: 1
Views: 985
Reputation: 2049
In your html you are using *ngIf="submitted && .... but I don't see submitted being set onSubmit()
onSubmit() {
this.submitted = true;
// rest of code goes here
}
you might consider adding this to the onSubmit() method
Also, you can check for validations once field is touched and dirty
*ngIf="(f.dirty || f.touched) && f.errors"
Upvotes: 2