-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReactiveForms.txt
59 lines (46 loc) · 1.94 KB
/
ReactiveForms.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
Reactive.component.html
------------------------------
<h1>Reactive Form Example</h1>
<form [formGroup]="userdetails" class="col-5 bg-info mx-auto" (submit)="save(userdetails)">
<div class="form-group">
<label>Enter Name:</label>
<input formControlName="uname">
<section *ngIf="userdetails.controls.uname.invalid" style="color: red">
<p *ngIf="userdetails.controls.uname.errors.required" name is required></p>
<p *ngIf="userdetails.controls.uname.errors.minlength" Atleast 4 characters></p>
<p>Error in Name Field</p>
</section>
<br>
<label>Enter Age</label>
<input formControlName="age">
<section *ngIf="userdetails.controls.age.invalid" style="color: red">
<p *ngIf="userdetails.controls.age.errors.required">Age is required</p>
<p *ngIf="userdetails.controls.age.errors.min"> After 18 Years</p>
<p *ngIf="userdetails.controls.age.errors.max">After 50 Years</p>
<p>Please Enter Age from 18 to 50 Years only</p>
</section>
<button>Register</button>
</div>
</form>
<pre>{{userdetails.value|json}}</pre>
--------------------------------------------------------
Reactive.component.ts
---------------------------
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
@Component({
selector: 'app-reactiveformexample',
templateUrl: './reactiveformexample.component.html',
styleUrls: ['./reactiveformexample.component.css']
})
export class ReactiveformexampleComponent implements OnInit {
constructor() { }
ngOnInit() {
}
//Model is created for Form
userdetails=new FormGroup({
uname:new FormControl('',[Validators.required, Validators.minLength(3)]),
age: new FormControl('',[Validators.required,Validators.min(18),Validators.max(50)])
})
}
--------------------------------------------------------------