Skip to content

Commit 4819631

Browse files
committed
fix(register): registration form upgrade to use reactive form directives
1 parent 02a35a6 commit 4819631

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

src/app/register/register.component.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ md-card {
2323

2424
md-input {
2525
display:block;
26-
margin-top:25px;
2726
}
2827

2928
md-spinner {

src/app/register/register.component.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ <h2>Angular2 Login Seed</h2>
99
<md-card-content>
1010
<h2>Create an account</h2>
1111

12-
<form *ngIf="!submitted" [ngFormModel]="form" (ngSubmit)="onSubmit()" #registerForm="ngForm">
13-
<md-input [ngFormModel]="name" ngControl="name" required placeholder="Name"></md-input>
14-
<md-input [ngFormModel]="username" ngControl="username" required placeholder="Username"></md-input>
15-
<md-input [ngFormModel]="email" ngControl="email" required placeholder="email@example.com"></md-input>
16-
<md-input [ngFormModel]="password" ngControl="password" required placeholder="Password" type="password"></md-input>
12+
<form *ngIf="!submitted" [formGroup]="form" (ngSubmit)="onSubmit()" #registerForm="ngForm">
13+
<md-input formControlName="name" required placeholder="Name"></md-input>
14+
<md-input formControlName="username" required placeholder="Username"></md-input>
15+
<md-input formControlName="email" required placeholder="email@example.com"></md-input>
16+
<md-input formControlName="password" required placeholder="Password" type="password"></md-input>
1717
<button type="submit" [disabled]="!registerForm.form.valid" md-raised-button color="primary">Register</button>
1818
</form>
1919

src/app/register/register.component.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, OnInit } from '@angular/core';
2-
import { NgForm, Validators, ControlGroup, Control } from '@angular/common';
2+
import { REACTIVE_FORM_DIRECTIVES, FormGroup, FormControl, Validators } from '@angular/forms';
33
import { Router } from '@angular/router';
44
import { MdButton } from '@angular2-material/button';
55
import { MdInput } from '@angular2-material/input';
@@ -21,19 +21,19 @@ import { UserService } from '../shared/services/user/user.service';
2121
selector: 'register',
2222
templateUrl: 'app/register/register.component.html',
2323
styleUrls: ['app/register/register.component.css'],
24-
directives: [MD_CARD_DIRECTIVES, MdButton, MdInput, MdSpinner]
24+
directives: [MD_CARD_DIRECTIVES, MdButton, MdInput, MdSpinner, REACTIVE_FORM_DIRECTIVES]
2525
})
2626

2727
export class RegisterComponent implements OnInit {
2828
title = 'Register';
2929
loginLink = '/login';
3030
githubLink = 'https://github.com/domfarolino/angular2-login-seed';
3131

32-
name: Control;
33-
username: Control;
34-
email: Control;
35-
password: Control;
36-
form: ControlGroup;
32+
name: FormControl;
33+
username: FormControl;
34+
email: FormControl;
35+
password: FormControl;
36+
form: FormGroup;
3737

3838
/**
3939
* Boolean used in telling the UI
@@ -56,19 +56,19 @@ export class RegisterComponent implements OnInit {
5656
/**
5757
* Initialize form Controls
5858
*/
59-
this.name = new Control('', Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(64)]));
60-
this.username = new Control('', Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(64)]));
61-
this.email = new Control('', Validators.compose([Validators.required, Validators.pattern("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")]));
62-
this.password = new Control('', Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(32)]));
59+
this.name = new FormControl('', Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(64)]));
60+
this.username = new FormControl('', Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(64)]));
61+
this.email = new FormControl('', Validators.compose([Validators.required, Validators.pattern("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")]));
62+
this.password = new FormControl('', Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(32)]));
6363

6464
/**
6565
* Initialize form
6666
*/
67-
this.form = new ControlGroup({
68-
name: this.name,
69-
username: this.username,
70-
email: this.email,
71-
password: this.password
67+
this.form = new FormGroup({
68+
'name': this.name,
69+
'username': this.username,
70+
'email': this.email,
71+
'password': this.password
7272
});
7373
}
7474

0 commit comments

Comments
 (0)