Day 3
Day 3
Now you can click on the login and registration button to redirect to the page. But it will load
complete page every time you click on the link. So we need to add Routerlink tag in the
header folder
<small class="form-text"
>This site uses Gravatar so if you want a profile image, use a Gravatar
email</small
>
</div>
<div>
<div class="form-group">
<input type="password" placeholder="Password" name="password" />
</div>
<div class="form-group">
<input
type="password"
placeholder="Confirm Password"
name="confirmPassword"
/>
</div>
</div>
Angular use its own form. So to add it you need to import formsModule in auth.module.ts
Now create the formBuider object in the register.component.ts
FormBuilder is used to store the form. It act like container for it.
You going to need constructor to use it
Now create the formGroup object and use it inside the constructor.
Connect form group with html page by form group directives
For this replace the “action” part with [formGroup]="registerForm"
In above case there are more than one validators (like required, minlength etc), so we have
created an array to store validators.
If there is only one validator, need to add validator only like
Name = [‘’, Validators.required]
But we need to verify the password and confirm password are matching or not
For that we will create util package and one file inside it
Write the code in passwordValidator.ts file
registerSubmit() {
if (this.registerForm.valid) {
//it will return true when all the validations are verified including
angular (length, reuqired, email) and custom (pasword matching)
console.log('Success' + this.registerForm.value);
//this will not be able to print the object, so write the following code
console.log('Success' + JSON.stringify(this.registerForm.value));
} else {
console.log(this.registerForm.errors);
//this.printErrors();
}
}
You can also see the password mismatch error if you enter different password
When password matches but other data does not clear the validation, it shows null value in
the console.