Skip to content

Commit 9f4fa1d

Browse files
authored
feat: add validationResult property to UI validation flow (#73)
1 parent 8969ede commit 9f4fa1d

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

Sources/ValidatorUI/Classes/IUIValidatable.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ public protocol IUIValidatable: AnyObject {
2626
/// The input value that needs to be validated.
2727
var inputValue: Input { get }
2828

29+
/// The most recent validation result, if any.
30+
///
31+
/// This value is updated automatically when calling `validate(rules:)`.
32+
/// Implementations may use this property to drive UI updates.
33+
var validationResult: ValidationResult? { get }
34+
2935
/// Validates the input value using a single rule.
3036
///
3137
/// - Parameter rule: A validation rule conforming to `IValidationRule`.
@@ -48,9 +54,10 @@ public protocol IUIValidatable: AnyObject {
4854

4955
// MARK: - Associated Object Keys
5056

51-
// Keys used for storing associated objects (validation rules and handlers)
57+
// Keys used for storing associated objects (validation rules, handlers, and a validation result)
5258
private nonisolated(unsafe) var kValidationRules: UInt8 = 0
5359
private nonisolated(unsafe) var kValidationHandler: UInt8 = 0
60+
private nonisolated(unsafe) var kValidationResult: UInt8 = 0
5461

5562
// Validator instance shared for UI validation
5663
// swiftlint:disable:next prefixed_toplevel_constant
@@ -78,6 +85,7 @@ public extension IUIValidatable {
7885
func validate(rules: [any IValidationRule<Input>]) -> ValidationResult {
7986
let result = validator.validate(input: inputValue, rules: rules)
8087
validationHandler?(result)
88+
validationResult = result
8189
return result
8290
}
8391

@@ -88,6 +96,24 @@ public extension IUIValidatable {
8896
validationRules.append(rule)
8997
}
9098

99+
/// The most recent validation result, if any.
100+
///
101+
/// This value is updated automatically when calling `validate(rules:)`.
102+
/// Implementations may use this property to drive UI updates.
103+
private(set) var validationResult: ValidationResult? {
104+
get {
105+
(objc_getAssociatedObject(self, &kValidationResult) as? AnyObject) as? ValidationResult
106+
}
107+
set {
108+
objc_setAssociatedObject(
109+
self,
110+
&kValidationResult,
111+
newValue as ValidationResult?,
112+
.OBJC_ASSOCIATION_RETAIN_NONATOMIC
113+
)
114+
}
115+
}
116+
91117
/// The array of validation rules associated with this UI element.
92118
var validationRules: [any IValidationRule<Input>] {
93119
get {

Sources/ValidatorUI/Classes/SUI/Managers/FormField/FormFieldManager/FormFieldManager.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public final class FormFieldManager: IFormFieldManager {
4040
/// The manager subscribes to the validator's publisher so that any changes
4141
/// in validation results automatically trigger re-evaluation of the form's overall validity.
4242
public func append(validator: some IFormValidationContainer) {
43-
// Subscribe to validation updates for this field
4443
validator
4544
.publisher
4645
.sink(receiveValue: { [weak self] _ in

0 commit comments

Comments
 (0)