Skip to content

Commit da5f6d5

Browse files
committed
feat: add Template Driven forms
1 parent b996668 commit da5f6d5

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

src/app/forms/template/template.component.html

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
<div class="container">
2-
<form novalidate>
2+
<form #form="ngForm" (ngSubmit)="signUp(form.value)" novalidate>
33
<div class="card card-signup">
44
<div class="card-header">Profile</div>
55
<div class="card-block">
66
<div class="form-group row">
77
<label for="inputEmail" class="col-sm-3 col-form-label">Email*</label>
88
<div class="col-sm-9">
99
<input type="email" class="form-control" id="inputEmail3" placeholder="[email protected]"
10-
name="email" required minlength="5">
10+
ngModel labsEmailValidator name="email" required minlength="5">
1111
</div>
1212
</div>
1313
<div class="form-group row">
1414
<label for="inputFirstName" class="col-sm-3 col-form-label">First Name*</label>
1515
<div class="col-sm-9">
1616
<input type="text" class="form-control" id="inputFirstName" placeholder=""
17-
name="firstName" required maxlength="25">
17+
ngModel pattern="[A-Za-z .]*" name="firstName" required maxlength="25">
1818
</div>
1919
</div>
2020
<div class="form-group row">
2121
<label for="inputLastName" class="col-sm-3 col-form-label">Last Name</label>
2222
<div class="col-sm-9">
2323
<input type="text" class="form-control" id="inputLastName" placeholder=""
24-
name="lastName" maxlength="25">
24+
ngModel name="lastName" maxlength="25">
2525
</div>
2626
</div>
2727
<fieldset class="form-group row">
2828
<legend class="col-form-legend col-sm-3">Gender</legend>
2929
<div class="col-sm-9 form-radio">
3030
<label class="custom-control custom-radio">
31-
<input id="inputMale" name="gender" type="radio" class="custom-control-input">
31+
<input id="inputMale" ngModel value="Male" name="gender" type="radio" class="custom-control-input">
3232
<span class="custom-control-indicator"></span>
3333
<span class="custom-control-description">Male</span>
3434
</label>
3535
<label class="custom-control custom-radio">
36-
<input id="inputFemale" name="gender" type="radio" class="custom-control-input">
36+
<input id="inputFemale" ngModel name="gender"value="Female" type="radio" class="custom-control-input">
3737
<span class="custom-control-indicator"></span>
3838
<span class="custom-control-description">Female</span>
3939
</label>
@@ -42,7 +42,8 @@
4242
<div class="form-group row">
4343
<div class="offset-sm-3 col-sm-9">
4444
<label class="custom-control custom-checkbox">
45-
<input type="checkbox" name="notrobot" class="custom-control-input" required>
45+
<input type="checkbox" ngModel name="notrobot"
46+
class="custom-control-input" required labsCheckboxCheckedValidator>
4647
<span class="custom-control-indicator"></span>
4748
<span class="custom-control-description">I am not a Robot*</span>
4849
</label>
@@ -52,54 +53,54 @@
5253
</div>
5354
<div class="card card-signup">
5455
<div class="card-header">Address</div>
55-
<div class="card-block">
56+
<div class="card-block" ngModelGroup="address">
5657
<div class="form-group row">
5758
<label for="inputAddressLine1" class="col-sm-3 col-form-label">Address Line 1*</label>
5859
<div class="col-sm-9">
5960
<input type="text" class="form-control" id="inputAddressLine1" placeholder="Room/Flat/Plot No. Locality"
60-
name="addressLine1" required>
61+
ngModel name="addressLine1" required>
6162
</div>
6263
</div>
6364
<div class="form-group row">
6465
<label for="inputAddressLine2" class="col-sm-3 col-form-label">Address Line 2</label>
6566
<div class="col-sm-9">
6667
<input type="text" class="form-control" id="inputAddressLine2" placeholder="Street, Area"
67-
name="addressLine2">
68+
ngModel name="addressLine2">
6869
</div>
6970
</div>
7071
<div class="row">
7172
<div class="form-group">
7273
<label for="inputCity" class="col-sm-3 col-form-label">City*</label>
7374
<div class="col-sm-4">
7475
<input type="text" class="form-control" id="inputCity" placeholder="City"
75-
name="city" required>
76+
ngModel name="city" required>
7677
</div>
7778
</div>
7879
<div class="form-group">
7980
<label for="inputZipCode" class="col-sm-1 col-form-label">Zip*</label>
8081
<div class="col-sm-4">
8182
<input type="number" class="form-control" id="inputZipCode" placeholder="Zip code"
82-
name="zipCode" required>
83+
ngModel name="zipCode" required>
8384
</div>
8485
</div>
8586
</div>
8687
<div class="form-group row" style="margin-top:1.2rem">
8788
<label for="inputLandmark" class="col-sm-3 col-form-label">Landmark</label>
8889
<div class="col-sm-9">
8990
<input type="text" class="form-control" id="inputLandmark" placeholder="Landmark"
90-
name="landmark" >
91+
ngModel name="landmark" >
9192
</div>
9293
</div>
9394
<fieldset class="form-group row">
9495
<legend class="col-form-legend col-sm-3">Type</legend>
9596
<div class="col-sm-9 form-radio">
9697
<label class="custom-control custom-radio">
97-
<input id="inputResidence" name="addressType" type="radio" class="custom-control-input">
98+
<input id="inputResidence" ngModel name="addressType" value="Residence" type="radio" class="custom-control-input">
9899
<span class="custom-control-indicator"></span>
99100
<span class="custom-control-description">Residence</span>
100101
</label>
101102
<label class="custom-control custom-radio">
102-
<input id="inputOffice" name="addressType" type="radio" class="custom-control-input">
103+
<input id="inputOffice" ngModel name="addressType" value="Office" type="radio" class="custom-control-input">
103104
<span class="custom-control-indicator"></span>
104105
<span class="custom-control-description">Office</span>
105106
</label>
@@ -109,7 +110,7 @@
109110
<div class="form-group">
110111
<label for="inputCountry" class="col-sm-3 col-form-label">Country</label>
111112
<div class="col-sm-6">
112-
<select class="custom-select" id="inputCountry" name="country">
113+
<select class="custom-select" id="inputCountry" name="country" ngModel>
113114
<option value="" selected>Select Country</option>
114115
<option value="ALA">Åland Islands</option>
115116
<option value="ALB">Albania</option>
@@ -365,10 +366,14 @@
365366
</div>
366367
</div>
367368
</div>
368-
<div class="form-group row">
369+
<div class="row">
369370
<div class="offset-sm-2 col-sm-9">
370-
<button type="submit" class="btn btn-primary">Sign up</button>
371+
<button type="submit" class="btn btn-primary" [disabled]="!form.valid">Sign up</button>
372+
Your form is {{form.valid ? 'valid' : 'invalid'}}
371373
</div>
372374
</div>
373375
</form>
374376
</div>
377+
378+
379+
<div class="jumbotron">{{form.value | json}}</div>

src/app/forms/template/template.component.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ import { Component } from '@angular/core';
44
selector: 'labs-template',
55
templateUrl: 'template.component.html'
66
})
7-
export class TemplateComponent {}
7+
export class TemplateComponent {
8+
signUp(data: any) {
9+
console.log('Submit the data: ', data);
10+
}
11+
}
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import { NgModule } from '@angular/core';
2-
import { TemplateComponent } from './template.component.ts';
2+
import { CommonModule } from '@angular/common';
3+
import { FormsModule } from '@angular/forms';
34

5+
import { TemplateComponent } from './template.component.ts';
46
import { moduleRoutes } from './template.routes.ts';
7+
import { Bootstrap4FormsModule } from '../../shared/bootstrap4/forms';
8+
import { ValidatorsModule } from '../../shared/validators';
59

610
@NgModule({
7-
imports: [ moduleRoutes ],
11+
imports: [
12+
moduleRoutes,
13+
CommonModule,
14+
FormsModule,
15+
Bootstrap4FormsModule,
16+
ValidatorsModule
17+
],
818
declarations: [ TemplateComponent ]
919
})
10-
export class TemplateModule {}
20+
export class TemplateModule { }

0 commit comments

Comments
 (0)