@@ -18,14 +18,15 @@ public struct FieldChecker {
1818 internal var numberOfCheck = 0
1919 public var errorMessage : String ?
2020
21- public var isFirstCheck : Bool { numberOfCheck == 1 }
21+ public var isFirstCheck : Bool { numberOfCheck == 0 }
2222
2323 public var valid : Bool {
2424 self . errorMessage == nil
2525 }
2626 public init ( errorMessage: String ? = nil ) {
2727 self . errorMessage = errorMessage
2828 }
29+
2930}
3031
3132@available ( iOS 13 , * )
@@ -38,7 +39,9 @@ public class FieldValidator<T> : ObservableObject where T : Hashable {
3839 @Published public var value : T
3940 {
4041 willSet {
41- self . doValidate ( newValue)
42+ if ( newValue != value) {
43+ self . doValidate ( value: newValue)
44+ }
4245 }
4346 didSet {
4447 self . bindValue = self . value
@@ -57,14 +60,19 @@ public class FieldValidator<T> : ObservableObject where T : Hashable {
5760 self . _checker = checker
5861 }
5962
60- public func doValidate( _ newValue: T ? = nil ) -> Void {
61-
62- self . checker. errorMessage =
63- ( newValue != nil ) ?
64- self . validator ( newValue! ) :
65- self . validator ( self . value )
66- self . checker. numberOfCheck += 1
63+ fileprivate func doValidate( value newValue: T ) -> Void {
64+ DispatchQueue . main. async {
65+ self . checker. errorMessage = self . validator ( newValue )
66+ self . checker. numberOfCheck += 1
67+ }
6768 }
69+
70+ public func doValidate( ) -> Void {
71+ DispatchQueue . main. async {
72+ self . checker. errorMessage = self . validator ( self . value )
73+ }
74+ }
75+
6876}
6977
7078
@@ -116,6 +124,7 @@ public struct TextFieldWithValidator : ViewWithFieldValidator {
116124 VStack {
117125 TextField ( title ?? " " , text: $field. value, onCommit: execIfValid ( self . onCommit) )
118126 . onAppear { // run validation on appear
127+ // print("\(type(of: self)) onAppear")
119128 self . field. doValidate ( )
120129 }
121130 }
@@ -148,9 +157,11 @@ public struct SecureFieldWithValidator : ViewWithFieldValidator {
148157 }
149158
150159 public var body : some View {
160+
151161 VStack {
152162 SecureField ( title ?? " " , text: $field. value, onCommit: execIfValid ( self . onCommit) )
153163 . onAppear { // run validation on appear
164+ // print("\(type(of: self)) onAppear")
154165 self . field. doValidate ( )
155166 }
156167 }
0 commit comments