From daed451c7afa9022f1ff273be08c8a29cf65a800 Mon Sep 17 00:00:00 2001 From: "Suresh, Kamlesh" Date: Fri, 13 Sep 2019 15:25:13 -0400 Subject: [PATCH] improvements --- MVMCoreUI/Atoms/TextFields/MFTextField.m | 2 +- .../FormValidationProtocol.swift | 11 ++-- MVMCoreUI/FormUIHelpers/FormValidator.swift | 54 +++++++++---------- 3 files changed, 31 insertions(+), 36 deletions(-) diff --git a/MVMCoreUI/Atoms/TextFields/MFTextField.m b/MVMCoreUI/Atoms/TextFields/MFTextField.m index 0e9be054..c52c4972 100644 --- a/MVMCoreUI/Atoms/TextFields/MFTextField.m +++ b/MVMCoreUI/Atoms/TextFields/MFTextField.m @@ -303,7 +303,7 @@ return; } - self.formText = [map string:KeyLabel];; + self.formText = [map string:KeyLabel]; self.groupName = [map string:@"groupName"]; self.errMessage = [map string:KeyErrorMessage]; self.fieldKey = [map string:KeyFieldKey]; diff --git a/MVMCoreUI/FormUIHelpers/FormValidationProtocol.swift b/MVMCoreUI/FormUIHelpers/FormValidationProtocol.swift index 2e109811..27f3f754 100644 --- a/MVMCoreUI/FormUIHelpers/FormValidationProtocol.swift +++ b/MVMCoreUI/FormUIHelpers/FormValidationProtocol.swift @@ -10,26 +10,27 @@ import Foundation @objc public protocol FormValidationProtocol: NSObjectProtocol { - // Getter method to get the FormValidator form the delegate (Mostly from the parent View Controller) + // Getter method to get the FormValidator from the delegate (Mostly from the parent View Controller) @objc optional func formValidatorModel() -> FormValidator? } -@objc public protocol FormValidationFormFieldProtocol: FormValidationProtocol { - // Used to check the validity of the field, to enable/disable the primary button. +@objc public protocol FormValidationFormFieldProtocol: FormValidationProtocol { + // Used to check the validity of the field. For example, to enable/disable the primary button. @objc func isValidField() -> Bool // The Field name key value pair for sending to server @objc func formFieldName() -> String? - // Returns the group name for validation + // Returns the group name for validation. The class should always return a value. @objc func formFieldGroupName() -> String? // The Field value key value pair for sending to server @objc func formFieldValue() -> Any? } -@objc public protocol FormValidationEnableDisableProtocol: FormValidationProtocol { +@objc public protocol FormValidationEnableDisableProtocol: FormValidationProtocol { + // Returns true if the button needs to be enabled/disabled based on the validation @objc func isValidationRequired() -> Bool // Based on the isValidField(), the fields which needs to be enabled can call this method diff --git a/MVMCoreUI/FormUIHelpers/FormValidator.swift b/MVMCoreUI/FormUIHelpers/FormValidator.swift index e1c2b13c..c3cc77fa 100644 --- a/MVMCoreUI/FormUIHelpers/FormValidator.swift +++ b/MVMCoreUI/FormUIHelpers/FormValidator.swift @@ -12,8 +12,9 @@ import MVMCore @objcMembers public class FormValidator: NSObject { - var delegate: FormValidationProtocol? var extraValidationBlock: (() -> Bool)? + var dummyGroupName = "dummyGroupName" + weak var delegate: FormValidationProtocol? var fieldMolecules: [FormValidationFormFieldProtocol] = [] var enableDisableMolecules: [FormValidationEnableDisableProtocol] = [] var radioButtonsModelByGroup: [String: RadioButtonModel] = [:] @@ -47,44 +48,37 @@ import MVMCore } public func enableByValidation() { - if enableDisableMolecules.count > 1 { - enableByGroup() - } else { - enableByFieldValidation() + for molecule in enableDisableMolecules { + if let requiredFeilds = molecule.requiredFields?(), requiredFeilds.count > 0 { + enableWithGroupName(requiredFeilds, molecule) + } else { + enableIgnoreGroupName(molecule) + } } } - public func enableByFieldValidation() { - guard let enableMolecule = enableDisableMolecules.first else { - return; + public func enableWithGroupName(_ requiredGroupList: [String], _ enableDisableMolecules: FormValidationEnableDisableProtocol) { + + var groupValidityMap: [String: Bool] = [:] + for molecule in fieldMolecules { + let valid = molecule.isValidField() + let groupName = molecule.formFieldGroupName() ?? dummyGroupName + groupValidityMap[groupName] = valid && (groupValidityMap[groupName] ?? true) } + var valid = false + for groupName in requiredGroupList { + valid = groupValidityMap[groupName] ?? false + } + enableDisableMolecules.enableField?(valid) + } + + public func enableIgnoreGroupName(_ enableDisableMolecules: FormValidationEnableDisableProtocol) { var valid = true for molecule in fieldMolecules { valid = valid && molecule.isValidField() } let enableField = valid && (extraValidationBlock?() ?? true) - enableMolecule.enableField?(enableField) + enableDisableMolecules.enableField?(enableField) } - - func enableByGroup() { - var groupValue: [String: Bool] = [:] - for molecule in fieldMolecules { - let valid = molecule.isValidField() - if let grouName = molecule.formFieldGroupName() { - groupValue[grouName] = valid && (groupValue[grouName] ?? true) - } - } - - for molecule in enableDisableMolecules { - var valid = false - for groupName in molecule.requiredFields?() ?? [] { - valid = groupValue[groupName] ?? false - } - molecule.enableField?(valid) - } - } - - } -