1
1
import { Component } from '@angular/core' ;
2
- import { FormBuilder , FormGroup , Validators } from '@angular/forms' ;
2
+ import { FormArray , FormBuilder , FormControl , FormGroup , Validators } from '@angular/forms' ;
3
+ import { DateRange , IRangeSliderValue } from 'igniteui-angular' ;
3
4
5
+
6
+ export interface User {
7
+ date : FormControl < string | Date | null > ,
8
+ time : FormControl < string | Date | null > ,
9
+ email : FormControl < string | null > ,
10
+ fullName : FormControl < string | null > ,
11
+ movie : FormControl < string | null > ,
12
+ phone : FormControl < number | null > ,
13
+ rating : FormControl < number | null > ,
14
+ checkbox : FormControl < boolean | null > ,
15
+ radio : FormControl ,
16
+ switch : FormControl < boolean | null > ,
17
+ range : FormControl < DateRange | null > ,
18
+ slider : FormControl < IRangeSliderValue | null >
19
+ calendar : FormControl < string | Date | null >
20
+ month : FormControl < string | Date | null >
21
+ genres : FormControl < string [ ] | null >
22
+ }
4
23
@Component ( {
5
24
selector : 'app-reactive-form' ,
6
25
styleUrls : [ './reactive-forms.component.scss' ] ,
7
26
templateUrl : './reactive-forms.component.html'
8
27
} )
9
28
export class ReactiveFormsSampleComponent {
10
29
public genres = [ ] ;
11
- public user : FormGroup ;
12
30
public minTime = '06:15:30' ;
13
31
public maxTime = '09:15:30' ;
32
+ public minValue = '0' ;
33
+ public maxValue = '100' ;
14
34
public minDate = new Date ( ) ;
15
35
public maxDate = new Date ( new Date ( this . minDate . getFullYear ( ) , this . minDate . getMonth ( ) , this . minDate . getDate ( ) + 14 ) ) ;
36
+ public user : FormGroup < User > ;
16
37
17
-
18
- constructor ( fb : FormBuilder ) {
19
- this . user = fb . group ( {
20
- date : [ '' , Validators . required ] ,
21
- time : [ '' , Validators . required ] ,
22
- email : [ '' , Validators . required ] ,
23
- fullName : [ '' , Validators . required ] ,
24
- genres : [ '' ] ,
25
- movie : [ '' , Validators . required ] ,
26
- phone : [ '' ]
38
+ constructor ( ) {
39
+ this . user = new FormGroup < User > ( {
40
+ date : new FormControl ( '' , Validators . required ) ,
41
+ time : new FormControl ( '' , Validators . required ) ,
42
+ email : new FormControl ( '' , [ Validators . required , Validators . email ] ) ,
43
+ fullName : new FormControl ( '' , [ Validators . required , Validators . pattern ( / ^ [ \p{ L} \p{ M} ' \- ] + $ / u) ] ) ,
44
+ movie : new FormControl ( '' , Validators . required ) ,
45
+ phone : new FormControl ( null , Validators . required ) ,
46
+ rating : new FormControl ( 2 , Validators . required ) ,
47
+ checkbox : new FormControl ( true , Validators . required ) ,
48
+ radio : new FormControl ( '' ) ,
49
+ switch : new FormControl ( false ) ,
50
+ range : new FormControl ( { start : new Date ( ) , end : new Date ( new Date ( ) . setDate ( new Date ( ) . getDate ( ) + 5 ) ) } ) ,
51
+ slider : new FormControl ( { lower : 5 , upper : 30 } ) ,
52
+ calendar : new FormControl ( null ) ,
53
+ month : new FormControl ( null ) ,
54
+ genres : new FormControl ( [ 'Action' , 'Adventure' , 'Comedy' ] )
27
55
} ) ;
28
-
29
56
this . genres = [
30
57
{ type : 'Action' , movies : [ 'The Matrix' , 'Kill Bill: Vol.1' , 'The Dark Knight Rises' ] } ,
31
58
{ type : 'Adventure' , movies : [ 'Interstellar' , 'Inglourious Basterds' , 'Inception' ] } ,
32
- {
33
- type : 'Comedy' , movies : [ 'Wild Tales' , 'In Bruges' , 'Three Billboards Outside Ebbing, Missouri' ,
34
- 'Untouchable' , '3 idiots' ]
35
- } ,
59
+ { type : 'Comedy' , movies : [ 'Wild Tales' , 'In Bruges' , 'Three Billboards Outside Ebbing, Missouri' , 'Untouchable' , '3 idiots' ] } ,
36
60
{ type : 'Crime' , movies : [ 'Training Day' , 'Heat' , 'American Gangster' ] } ,
37
61
{ type : 'Drama' , movies : [ 'Fight Club' , 'A Beautiful Mind' , 'Good Will Hunting' , 'City of God' ] } ,
38
62
{ type : 'Biography' , movies : [ 'Amadeus' , 'Bohemian Rhapsody' ] } ,
@@ -43,6 +67,17 @@ export class ReactiveFormsSampleComponent {
43
67
{ type : 'Thriller' , movies : [ 'The Usual Suspects' ] } ,
44
68
{ type : 'Western' , movies : [ 'Django Unchained' ] }
45
69
] ;
70
+
71
+ }
72
+
73
+ public get email ( ) {
74
+ return this . user . get ( 'email' ) ;
75
+ }
76
+ public get phone ( ) {
77
+ return this . user . get ( 'phone' ) ;
78
+ }
79
+ public get fullName ( ) {
80
+ return this . user . get ( 'fullName' ) ;
46
81
}
47
82
48
83
public onSubmit ( ) {
0 commit comments