Skip to content

Commit 5c18976

Browse files
committed
Reactive form controls
1 parent 0322ba5 commit 5c18976

37 files changed

+508
-900
lines changed

README.md

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,82 @@
1-
# Flipflop Quiz game
1+
# Run Application
2+
```
3+
ng serve
4+
localhost:4200/
5+
```
6+
7+
![Dynamic form controls](ss.png)
8+
9+
# Reactive form controls (Taken help from these tutorials)
210

3-
create an Interface class for Quiz data by running following command defining the type of values for quiz.
11+
https://stackblitz.com/edit/dynamic-form-generate-from-json-angular-2-reactive-form?file=src%2Fapp%2Fapp.component.ts
12+
https://medium.com/@fabiokounang/angular-6-dynamic-form-with-reactive-form-controls-43a10176a9f1
13+
https://dzone.com/articles/how-to-create-custom-validators-in-angular
14+
https://jasonwatmore.com/post/2018/05/10/angular-6-reactive-forms-validation-example
15+
https://stackblitz.com/edit/angular-6-reactive-form-validation?file=app%2Fapp.component.html
16+
17+
# Custom validator in Angular
18+
19+
Angular provides us many useful validators, including required, minLength, maxLength, and pattern. These validators are part of the Validators class, which comes with the @angular/forms package.
20+
21+
Let's assume you want to add a required validation to the email control and a maxLength validation to the password control. Here's how you do that:
22+
```
23+
this.loginForm = new FormGroup({
24+
email: new FormControl(null, [Validators.required]),
25+
password: new FormControl(null, [Validators.required, Validators.maxLength(8)]),
26+
age: new FormControl(null)
27+
});
28+
```
29+
To work with validators, make sure to import them into the component class:
430

531
```
6-
ng generate class models/Quiz
32+
import { FormGroup, FormControl, Validators } from '@angular/forms';
733
```
834

9-
# Run Application
35+
On the template, you can use validators to show or hide an error message. Essentially, you are reading formControl using the get() method and checking whether it has an error or not using the hasError() method. You are also checking whether formControl is touched or not using the touched property.
36+
37+
### Validate form on submission in reactive-form
38+
39+
You can actually achieve this already with the submitted flag on the top level form directive. create form directive using #formDir="ngForm" and then check
40+
form is submitted or not using "formDir.submitted" checking. "formDir.submitted" will return you boolean value true or false.
41+
42+
43+
#### Static reactive form validation (Email)
44+
45+
```
46+
<div class="col-12 form-group">
47+
<label>email</label>
48+
<input type="email" formControlName="email" class="form-control" [ngClass]="{'is-invalid' : registerForm.get(form.key).errors && formDir.submitted }">
49+
<div *ngIf="!registerForm.get(form.key).valid && registerForm.get(form.key).errors && formDir.submitted">
50+
<span class="error" *ngIf="registerForm.get(form.key).errors.hasOwnProperty('required')">email is required</span>
51+
<span class="error" *ngIf="registerForm.get(form.key).errors.hasOwnProperty('email')">email must be valid</span>
52+
</div>
53+
</div>
54+
```
55+
56+
#### Dynamic reactive form validation (Email)
57+
58+
```
59+
<div class="col-12 form-group">
60+
<label>{{form.key}}</label>
61+
<input [type]="form.input" [formControlName]="form.key" class="form-control" [ngClass]="{'is-invalid' : registerForm.get(form.key).errors && formDir.submitted }">
62+
<div *ngIf="!registerForm.get(form.key).valid && registerForm.get(form.key).errors && formDir.submitted">
63+
<div *ngFor="let err of form.valids; let k = index">
64+
<span class="error" *ngIf="registerForm.get(form.key).errors.hasOwnProperty(err.valid)">{{err.error}}</span>
65+
</div>
66+
</div>
67+
</div>
68+
```
69+
70+
OR
71+
72+
```
73+
<div class="col-12 form-group">
74+
<label>{{form.key}}</label>
75+
<input [type]="form.input" [formControlName]="form.key" class="form-control" [ngClass]="{'is-invalid' : registerForm.get(form.key).errors && formDir.submitted }">
76+
<div *ngIf="!registerForm.get(form.key).valid && registerForm.get(form.key).errors && formDir.submitted">
77+
<div *ngFor="let err of form.valids; let k = index">
78+
<span class="error" *ngIf="registerForm.get(form.key).hasError(err.valid)">{{err.error}}</span>
79+
</div>
80+
</div>
81+
</div>
1082
```
11-
ng serve
12-
localhost:4200/
13-
```

angular.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": 1,
44
"newProjectRoot": "projects",
55
"projects": {
6-
"flipflop": {
6+
"dynamic-form-controls": {
77
"projectType": "application",
88
"schematics": {},
99
"root": "",
@@ -13,7 +13,7 @@
1313
"build": {
1414
"builder": "@angular-devkit/build-angular:browser",
1515
"options": {
16-
"outputPath": "dist/flipflop",
16+
"outputPath": "dist/dynamic-form-controls",
1717
"index": "src/index.html",
1818
"main": "src/main.ts",
1919
"polyfills": "src/polyfills.ts",
@@ -58,18 +58,18 @@
5858
"serve": {
5959
"builder": "@angular-devkit/build-angular:dev-server",
6060
"options": {
61-
"browserTarget": "flipflop:build"
61+
"browserTarget": "dynamic-form-controls:build"
6262
},
6363
"configurations": {
6464
"production": {
65-
"browserTarget": "flipflop:build:production"
65+
"browserTarget": "dynamic-form-controls:build:production"
6666
}
6767
}
6868
},
6969
"extract-i18n": {
7070
"builder": "@angular-devkit/build-angular:extract-i18n",
7171
"options": {
72-
"browserTarget": "flipflop:build"
72+
"browserTarget": "dynamic-form-controls:build"
7373
}
7474
},
7575
"test": {
@@ -106,15 +106,15 @@
106106
"builder": "@angular-devkit/build-angular:protractor",
107107
"options": {
108108
"protractorConfig": "e2e/protractor.conf.js",
109-
"devServerTarget": "flipflop:serve"
109+
"devServerTarget": "dynamic-form-controls:serve"
110110
},
111111
"configurations": {
112112
"production": {
113-
"devServerTarget": "flipflop:serve:production"
113+
"devServerTarget": "dynamic-form-controls:serve:production"
114114
}
115115
}
116116
}
117117
}
118118
}},
119-
"defaultProject": "flipflop"
119+
"defaultProject": "dynamic-form-controls"
120120
}

e2e/src/app.e2e-spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('workspace-project App', () => {
1010

1111
it('should display welcome message', () => {
1212
page.navigateTo();
13-
expect(page.getTitleText()).toEqual('Welcome to flipflop!');
13+
expect(page.getTitleText()).toEqual('Welcome to dynamic-form-controls!');
1414
});
1515

1616
afterEach(async () => {

karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = function (config) {
1616
clearContext: false // leave Jasmine Spec Runner output visible in browser
1717
},
1818
coverageIstanbulReporter: {
19-
dir: require('path').join(__dirname, './coverage/flipflop'),
19+
dir: require('path').join(__dirname, './coverage/dynamic-form-controls'),
2020
reports: ['html', 'lcovonly', 'text-summary'],
2121
fixWebpackSourcePaths: true
2222
},

0 commit comments

Comments
 (0)