@@ -15,10 +15,28 @@ struct ContentView: View {
1515 @State private var lastName : String = " "
1616 @State private var mobileNumber : String = " "
1717 @State private var email : String = " "
18+ @State private var isValidEmail : Bool = false
1819 @State private var password : String = " "
20+ @State private var date : Date = Date ( )
21+ @State private var birthDate : String = " "
22+ @State private var showDatePicker : Bool = false
1923
2024 @State private var isPasswordShow : Bool = false
2125
26+ private var selectedDate : Binding < Date > {
27+ Binding < Date > ( get: { self . date} , set : {
28+ self . date = $0
29+ self . setDateFormatterString ( )
30+ } )
31+ }
32+
33+ private func setDateFormatterString( ) {
34+ let formatter = DateFormatter ( )
35+ formatter. dateFormat = " dd - MMMM, yyyy "
36+
37+ self . birthDate = formatter. string ( from: self . date)
38+ }
39+
2240 var body : some View {
2341 VStack {
2442
@@ -28,17 +46,37 @@ struct ContentView: View {
2846 } ) {
2947
3048 }
31- . floatingStyle ( ThemeTextFieldStyle ( ) )
32- . modifier ( ThemeTextField ( ) )
49+ . isShowError ( true )
50+ . addValidations ( [ . init( condition: firstName. isValid ( . alphabet) , errorMessage: " Invalid Name " ) ,
51+ . init( condition: firstName. count >= 2 , errorMessage: " Minimum two character long " )
52+ ] )
53+ . floatingStyle ( ThemeTextFieldStyle ( ) )
54+ . modifier ( ThemeTextField ( ) )
3355
3456
3557 FloatingLabelTextField ( $lastName, placeholder: " Last Name " , editingChanged: { ( isChanged) in
3658
3759 } ) {
3860
3961 }
40- . floatingStyle ( ThemeTextFieldStyle2 ( ) )
41- . modifier ( ThemeTextField ( ) )
62+ . isShowError ( true )
63+ . addValidations ( [ . init( condition: lastName. isValid ( . alphabet) , errorMessage: " Invalid Name " ) ,
64+ . init( condition: lastName. count >= 2 , errorMessage: " Minimum two character long " )
65+ ] )
66+ . floatingStyle ( ThemeTextFieldStyle2 ( ) )
67+ . modifier ( ThemeTextField ( ) )
68+ }
69+
70+ FloatingLabelTextField ( $birthDate, placeholder: " Birth Date " , editingChanged: { ( isChanged) in
71+ self . showDatePicker = isChanged
72+ } ) {
73+
74+ }
75+ . modifier ( ThemeTextField ( ) )
76+
77+ if showDatePicker {
78+ DatePicker ( " " , selection: selectedDate,
79+ displayedComponents: . date)
4280 }
4381
4482 FloatingLabelTextField ( $mobileNumber, placeholder: " Phone Number " , editingChanged: { ( isChanged) in
@@ -48,14 +86,16 @@ struct ContentView: View {
4886 }
4987 . keyboardType ( . phonePad)
5088 . modifier ( ThemeTextField ( ) )
51-
52- FloatingLabelTextField ( $email, placeholder: " Email " , editingChanged: { ( isChanged) in
89+ FloatingLabelTextField ( $email, validtionChecker: $isValidEmail, placeholder: " Email " , editingChanged: { ( isChanged) in
5390
5491 } ) {
5592
5693 }
57- . keyboardType ( . emailAddress)
58- . modifier ( ThemeTextField ( ) )
94+ . addValidations ( [ . init( condition: email. isValid ( . email) , errorMessage: " Invalid Email " )
95+ ] )
96+ . isShowError ( true )
97+ . keyboardType ( . emailAddress)
98+ . modifier ( ThemeTextField ( ) )
5999
60100 FloatingLabelTextField ( $password, placeholder: " Password " , editingChanged: { ( isChanged) in
61101
@@ -78,6 +118,14 @@ struct ContentView: View {
78118 // Text(password)
79119 Button ( action: {
80120 self . endEditing ( true )
121+
122+ if self . isValidEmail {
123+ print ( " Valid field " )
124+
125+ } else {
126+ print ( " Invalid field " )
127+ }
128+
81129 } ) {
82130 Text ( " Create " )
83131 }
@@ -94,13 +142,13 @@ struct ContentView: View {
94142//MARK: Create floating style
95143struct ThemeTextFieldStyle : FloatingLabelTextFieldStyle {
96144 func body( content: FloatingLabelTextField ) -> FloatingLabelTextField {
97- content. titleColor ( . red )
145+ content. titleColor ( . black )
98146 }
99147}
100148
101149struct ThemeTextFieldStyle2 : FloatingLabelTextFieldStyle {
102150 func body( content: FloatingLabelTextField ) -> FloatingLabelTextField {
103- content. titleColor ( . green)
151+ content. titleColor ( . black ) . errorColor ( . init ( UIColor . green) )
104152 }
105153}
106154
0 commit comments