From c961e570ece4798d3726b6cdb96d0d5fa2dd1594 Mon Sep 17 00:00:00 2001 From: Andreas Bauer Date: Fri, 3 Nov 2023 16:33:59 -0700 Subject: [PATCH] Make isDisplayingValidationErrors based on the validationResults --- Sources/SpeziValidation/ValidationEngine.swift | 6 ++++-- Sources/SpeziValidation/ValidationModifier.swift | 4 ++++ Sources/SpeziValidation/Views/VerifiableTextField.swift | 5 ----- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Sources/SpeziValidation/ValidationEngine.swift b/Sources/SpeziValidation/ValidationEngine.swift index 019d48a..9d884f2 100644 --- a/Sources/SpeziValidation/ValidationEngine.swift +++ b/Sources/SpeziValidation/ValidationEngine.swift @@ -56,11 +56,13 @@ public class ValidationEngine: Identifiable { /// Flag that indicates if ``displayedValidationResults`` returns any ``FailedValidationResult``. @MainActor public var isDisplayingValidationErrors: Bool { + let gotResults = !validationResults.isEmpty + if configuration.contains(.hideFailedValidationOnEmptySubmit) { - return !inputValid && (source == .manual || !inputWasEmpty) + return gotResults && (source == .manual || !inputWasEmpty) } - return !inputValid + return gotResults } diff --git a/Sources/SpeziValidation/ValidationModifier.swift b/Sources/SpeziValidation/ValidationModifier.swift index 32c784a..50ad9dd 100644 --- a/Sources/SpeziValidation/ValidationModifier.swift +++ b/Sources/SpeziValidation/ValidationModifier.swift @@ -40,6 +40,10 @@ struct ValidationModifier: ViewModifier { .onChange(of: input) { validation.submit(input: input, debounce: true) } + .onSubmit(of: .text) { + // here we just make sure that we submit it without a debounce + validation.submit(input: input) + } } } diff --git a/Sources/SpeziValidation/Views/VerifiableTextField.swift b/Sources/SpeziValidation/Views/VerifiableTextField.swift index ccd3049..67bb3e2 100644 --- a/Sources/SpeziValidation/Views/VerifiableTextField.swift +++ b/Sources/SpeziValidation/Views/VerifiableTextField.swift @@ -42,11 +42,6 @@ public struct VerifiableTextField: View { SecureField(text: $text, label: { label }) } } - .onSubmit { - // the validation modifier automatically submits the text, - // here we just make sure that we submit it without a debounce - validationEngine.submit(input: text) - } HStack { ValidationResultsView(results: validationEngine.displayedValidationResults)