From bdaa4b5ea716889e3fca3a68c75826780503a4f9 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Tue, 30 Jul 2024 11:09:40 -0500 Subject: [PATCH] added extra check to ensure you aren't duplicating rules Signed-off-by: Matt Bruce --- MVMCoreUI/FormUIHelpers/FormValidator.swift | 6 ++++++ .../FormUIHelpers/Rules/Rules/RuleVDSModel.swift | 14 +++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/MVMCoreUI/FormUIHelpers/FormValidator.swift b/MVMCoreUI/FormUIHelpers/FormValidator.swift index b01c7ad4..e68b87a9 100644 --- a/MVMCoreUI/FormUIHelpers/FormValidator.swift +++ b/MVMCoreUI/FormUIHelpers/FormValidator.swift @@ -56,6 +56,12 @@ import MVMCore //find the group if let formGroup = formRules?.first(where: {$0.groupName == field.groupName}) { + var appendingRules = [RulesProtocol]() + internalRules.forEach { internalRule in + if !formGroup.rules.contains(where: { internalRule.type == $0.type && internalRule.fields == $0.fields } ) { + appendingRules.append(internalRule) + } + } formGroup.rules.append(contentsOf: internalRules) } else { //create the new group diff --git a/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleVDSModel.swift b/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleVDSModel.swift index e4cfb2aa..5443cda4 100644 --- a/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleVDSModel.swift +++ b/MVMCoreUI/FormUIHelpers/Rules/Rules/RuleVDSModel.swift @@ -9,12 +9,15 @@ import Foundation import VDS -open class VDSRuleBase: RuleAnyModelProtocol { +open class VDSRuleBase: RuleAnyModelProtocol { open var ruleId: String? + open var ruleType: String open var errorMessage: [String : String]? open var fields = [String]() - public init(){} - + public init(){ + ruleType = Self.identifier + } + public var type: String { ruleType } open func isValid(_ formField: any FormFieldProtocol) -> Bool { fatalError() } @@ -26,7 +29,7 @@ public class RuleVDSModel: VDSRuleBase { // MARK: - Properties //-------------------------------------------------- public var rule: AnyRule - + //-------------------------------------------------- // MARK: - Initializer //-------------------------------------------------- @@ -34,8 +37,8 @@ public class RuleVDSModel: VDSRuleBase { public init(field: String, rule: AnyRule) { self.rule = rule super.init() + self.ruleType = rule.ruleType self.fields = [field] - self.ruleId = "\(rule.self)-\(Int.random(in: 1...1000))" } required init(from decoder: any Decoder) throws { @@ -57,3 +60,4 @@ public class RuleVDSModel: VDSRuleBase { return valid } } +