1
+
1
2
import { Component , OnInit } from '@angular/core' ;
3
+ import { FormGroup , FormBuilder , Validators , AbstractControl , ValidatorFn , FormArray } from '@angular/forms' ;
4
+ import 'rxjs/add/operator/debounceTime' ;
5
+
6
+
7
+ // here function start
8
+ function emailMatcher ( c : AbstractControl ) : { [ key : string ] : boolean } | null {
9
+ let emailControl = c . get ( 'email' ) ;
10
+ let confirmControl = c . get ( 'confirmEmail' ) ;
11
+ if ( emailControl . pristine || confirmControl . pristine ) {
12
+ return null ;
13
+ }
14
+ if ( emailControl . value === confirmControl . value ) {
15
+ return null ;
16
+ }
17
+ return { 'match' : true } ;
18
+ }
19
+
20
+ function ratingRange ( min : number , max : number ) : ValidatorFn {
21
+ return ( c : AbstractControl ) : { [ key : string ] : boolean } | null => {
22
+ if ( c . value !== undefined && ( isNaN ( c . value ) || c . value < min || c . value > max ) ) {
23
+ return { 'range' : true } ;
24
+ } ;
25
+ return null ;
26
+ } ;
27
+ }
28
+
29
+ // here function end
2
30
3
31
@Component ( {
4
32
selector : 'app-register-client' ,
@@ -7,9 +35,106 @@ import { Component, OnInit } from '@angular/core';
7
35
} )
8
36
export class RegisterClientComponent implements OnInit {
9
37
10
- constructor ( ) { }
38
+ private clientMessage = {
39
+ title : 'Client Registration Form' ,
40
+ validationMessages :{
41
+ firtName :{
42
+ required : 'Please enter your first name.' ,
43
+ minlength : 'The first name must be longer than 3 characters.'
44
+ } ,
45
+ lastName : {
46
+ required : 'Please enter your last name.' ,
47
+ maxlength : 'The last name must be not longer than 50 characters.'
48
+ } ,
49
+ email : {
50
+ required : 'Please enter your email address.' ,
51
+ pattern : 'Please enter a valid email address.' ,
52
+ minlength : 'Please enter at least 4 characters.'
53
+ } ,
54
+ confirmEmail : {
55
+ required : 'Please confirm your email address.' ,
56
+ match : 'The confirmation does not match the email address.'
57
+ } ,
58
+ phone : {
59
+ required :'Please enter your phone number.'
60
+ } ,
61
+ rating : {
62
+ range : 'Please rate your experience from 1 to 5.'
63
+ }
64
+ }
65
+ }
66
+
67
+
68
+ customerForm : FormGroup ;
69
+ emailMessage : string ;
70
+ private validationMessages = {
71
+ required : 'Please enter your email address.' ,
72
+ pattern : 'Please enter a valid email address.' ,
73
+ minlength : 'Please enter at least 4 characters.'
74
+ } ;
75
+
76
+
77
+ constructor ( private fb : FormBuilder ) { }
11
78
12
79
ngOnInit ( ) {
80
+ this . customerForm = this . fb . group ( {
81
+ firstName : [ '' , [ Validators . required , Validators . minLength ( 3 ) ] ] ,
82
+ lastName : [ '' , [ Validators . required , Validators . maxLength ( 50 ) ] ] ,
83
+ emailGroup : this . fb . group ( {
84
+ email : [ '' , [ Validators . required , Validators . pattern ( '[a-z0-9._%+-]+@[a-z0-9.-]+' ) ,
85
+ Validators . minLength ( 4 ) ] ] ,
86
+ confirmEmail : [ '' , Validators . required ] ,
87
+ } , { validator : emailMatcher } ) ,
88
+ phone : '' ,
89
+ sendOtp : 'email' ,
90
+ rating : [ '' , ratingRange ( 1 , 5 ) ] ,
91
+ sendCatalog : [ true ]
92
+
93
+ } )
94
+
95
+ // sendOtp
96
+ this . customerForm . get ( 'sendOtp' ) . valueChanges
97
+ . subscribe ( value => {
98
+ this . setNotification ( value )
99
+ } ) ;
100
+
101
+ // email id message get here
102
+ const emailControl = this . customerForm . get ( 'emailGroup.email' ) ;
103
+ emailControl . valueChanges . debounceTime ( 1000 ) . subscribe ( value => {
104
+ console . log ( value ) ;
105
+ this . setMessage ( emailControl )
106
+ } ) ;
107
+ }
108
+
109
+ // set Message
110
+ setMessage ( c : AbstractControl ) : void {
111
+ this . emailMessage = '' ;
112
+ if ( ( c . touched || c . dirty ) && c . errors ) {
113
+ this . emailMessage = Object . keys ( c . errors ) . map ( key =>
114
+ this . validationMessages [ key ] ) . join ( ' ' ) ;
115
+ }
116
+ console . log ( 'c' + c ) ;
117
+ console . log ( 'c.touched - ' + c . touched ) ;
118
+ console . log ( 'c.dirty - ' + c . dirty ) ;
119
+ console . log ( 'c.errors - ' + c . errors ) ;
120
+ console . log ( c . errors ) ;
121
+ console . log ( c ) ;
122
+ }
123
+
124
+ // sendOtp code here
125
+ setNotification ( notifyVia : string ) : void {
126
+ const phoneControl = this . customerForm . get ( 'phone' ) ;
127
+ if ( notifyVia === 'text' ) {
128
+ phoneControl . setValidators ( Validators . required ) ;
129
+ } else {
130
+ phoneControl . clearValidators ( ) ;
131
+ }
132
+ phoneControl . updateValueAndValidity ( ) ;
133
+ }
134
+
135
+ save ( ) : void {
136
+ console . log ( this . customerForm ) ;
137
+ console . log ( 'Saved: ' + JSON . stringify ( this . customerForm . value ) ) ;
13
138
}
14
139
15
140
}
0 commit comments