77
88import SwiftUI
99
10- public struct FormField < Value : Hashable , Rule : ValidationRule , Content: View > : View where Value == Rule . Value {
11- @Binding private var value : Value
12- @ViewBuilder private let content : ( [ Rule ] ) -> Content
10+ public struct FormField < Content: View > : View {
11+ @Binding private var value : String
12+ @ViewBuilder private let content : ( [ ValidationRule ] ) -> Content
1313
14- @State private var failedValidationRules : [ Rule ] = [ ]
14+ @State private var failedValidationRules : [ ValidationRule ] = [ ]
1515
1616 // Fields Focus
1717 @FocusState private var isFocused : Bool
1818 @State private var id : String = UUID ( ) . uuidString
1919 @Environment ( \. focusedFieldId) var currentFocusedFieldId
2020
2121 // ValidateInput
22- private let validator : FieldValidator < Rule >
22+ private let validator : FieldValidator
2323 @Environment ( \. errorHideBehaviour) var errorHideBehaviour
2424 @Environment ( \. validationBehaviour) var validationBehaviour
2525
2626 public init (
27- value: Binding < Value > ,
28- rules: [ Rule ] = [ ] ,
29- @ViewBuilder content: @escaping ( [ Rule ] ) -> Content
27+ value: Binding < String > ,
28+ rules: [ ValidationRule ] = [ ] ,
29+ @ViewBuilder content: @escaping ( [ ValidationRule ] ) -> Content
3030 ) {
3131 self . _value = value
3232 self . content = content
@@ -46,7 +46,7 @@ public struct FormField<Value: Hashable, Rule: ValidationRule, Content: View>: V
4646 value: [
4747 // Замыкание для каждого филда вызывается FormValidator'ом из FormView для валидации по требованию
4848 FieldState ( id: id, isFocused: isFocused) {
49- let failedRules = validator. validate ( value: value)
49+ let failedRules = await validator. validate ( value: value, isNeedToCheckExternal : true )
5050 failedValidationRules = failedRules
5151
5252 return failedRules. isEmpty
@@ -57,23 +57,27 @@ public struct FormField<Value: Hashable, Rule: ValidationRule, Content: View>: V
5757
5858 // Fields Validation
5959 . onChange ( of: value) { newValue in
60- if errorHideBehaviour == . onValueChanged {
61- failedValidationRules = . empty
62- }
63-
64- if validationBehaviour == . onFieldValueChanged {
65- failedValidationRules = validator. validate ( value: newValue)
60+ Task { @MainActor in
61+ if errorHideBehaviour == . onValueChanged {
62+ failedValidationRules = . empty
63+ }
64+
65+ if validationBehaviour == . onFieldValueChanged {
66+ failedValidationRules = await validator. validate ( value: newValue, isNeedToCheckExternal: false )
67+ }
6668 }
6769 }
6870 . onChange ( of: isFocused) { newValue in
69- if errorHideBehaviour == . onFocusLost && newValue == false {
70- failedValidationRules = . empty
71- } else if errorHideBehaviour == . onFocus && newValue == true {
72- failedValidationRules = . empty
73- }
74-
75- if validationBehaviour == . onFieldFocusLost && newValue == false {
76- failedValidationRules = validator. validate ( value: value)
71+ Task { @MainActor in
72+ if errorHideBehaviour == . onFocusLost && newValue == false {
73+ failedValidationRules = . empty
74+ } else if errorHideBehaviour == . onFocus && newValue == true {
75+ failedValidationRules = . empty
76+ }
77+
78+ if validationBehaviour == . onFieldFocusLost && newValue == false {
79+ failedValidationRules = await validator. validate ( value: value, isNeedToCheckExternal: false )
80+ }
7781 }
7882 }
7983 }
0 commit comments