diff --git a/MVMCoreUI/Atoms/Buttons/PrimaryButton.h b/MVMCoreUI/Atoms/Buttons/PrimaryButton.h index 99929430..be780b0f 100644 --- a/MVMCoreUI/Atoms/Buttons/PrimaryButton.h +++ b/MVMCoreUI/Atoms/Buttons/PrimaryButton.h @@ -26,6 +26,7 @@ typedef enum : NSUInteger { static CGFloat const PrimaryButtonHeight = 42.0; static CGFloat const PrimaryButtonSmallHeight = 30.0; + @interface PrimaryButton : MFCustomButton @property (nonatomic, readonly, assign) PrimaryButtonType primaryButtonType; //use reset function to set diff --git a/MVMCoreUI/Atoms/Buttons/PrimaryButton.m b/MVMCoreUI/Atoms/Buttons/PrimaryButton.m index fa719012..daeb2751 100644 --- a/MVMCoreUI/Atoms/Buttons/PrimaryButton.m +++ b/MVMCoreUI/Atoms/Buttons/PrimaryButton.m @@ -13,6 +13,7 @@ #import "MFStyler.h" #import "UIColor+MFConvenience.h" #import + @import MVMCore.MVMCoreDispatchUtility; @import MVMCore.MVMCoreGetterUtility; @import MVMCore.NSDictionary_MFConvenience; @@ -667,6 +668,9 @@ } - (void)setWithJSON:(NSDictionary *)json delegateObject:(MVMCoreUIDelegateObject *)delegateObject additionalData:(NSDictionary *)additionalData { + + self.validationRequired = [json boolForKey:@"validationRequired"]; + self.requiredFieldsList = [json array:@"requiredFields"]; [FormValidator setupValidationWithMolecule:self delegate:delegateObject.formValidationProtocol]; self.primaryButtonType = PrimaryButtonTypeCustom; @@ -697,8 +701,6 @@ if ((color = [json string:@"disabledBorderColor"])) { self.disabledBorderColor = [UIColor mfGetColorForHex:color]; } - self.validationRequired = [json boolForKey:@"validationRequired"]; - self.requiredFieldsList = [json array:@"requiredFields"]; NSString *size = [json string:@"size"]; if ([size isEqualToString:@"small"]) { @@ -777,6 +779,10 @@ #pragma mark - FormValidationEnableDisableProtocol +- (BOOL) isValidationRequired { + return self.validationRequired; +} + - (NSArray *)requiredFields { return self.requiredFieldsList; } diff --git a/MVMCoreUI/FormUIHelpers/FormValidationProtocol.swift b/MVMCoreUI/FormUIHelpers/FormValidationProtocol.swift index 0af41b42..2e109811 100644 --- a/MVMCoreUI/FormUIHelpers/FormValidationProtocol.swift +++ b/MVMCoreUI/FormUIHelpers/FormValidationProtocol.swift @@ -29,6 +29,9 @@ import Foundation } @objc public protocol FormValidationEnableDisableProtocol: FormValidationProtocol { + + @objc func isValidationRequired() -> Bool + // Based on the isValidField(), the fields which needs to be enabled can call this method @objc optional func enableField(_ enable: Bool) diff --git a/MVMCoreUI/FormUIHelpers/FormValidator.swift b/MVMCoreUI/FormUIHelpers/FormValidator.swift index 821db784..e1c2b13c 100644 --- a/MVMCoreUI/FormUIHelpers/FormValidator.swift +++ b/MVMCoreUI/FormUIHelpers/FormValidator.swift @@ -13,6 +13,7 @@ import MVMCore @objcMembers public class FormValidator: NSObject { var delegate: FormValidationProtocol? + var extraValidationBlock: (() -> Bool)? var fieldMolecules: [FormValidationFormFieldProtocol] = [] var enableDisableMolecules: [FormValidationEnableDisableProtocol] = [] var radioButtonsModelByGroup: [String: RadioButtonModel] = [:] @@ -21,8 +22,9 @@ import MVMCore if let molecule = molecule as? FormValidationFormFieldProtocol { fieldMolecules.append(molecule) } - if let molecule = molecule as? FormValidationEnableDisableProtocol { - enableDisableMolecules.append(molecule) + if let moleculeT = molecule as? FormValidationEnableDisableProtocol, + moleculeT.isValidationRequired() { + enableDisableMolecules.append(moleculeT) } } @@ -45,6 +47,27 @@ import MVMCore } public func enableByValidation() { + if enableDisableMolecules.count > 1 { + enableByGroup() + } else { + enableByFieldValidation() + } + } + + public func enableByFieldValidation() { + guard let enableMolecule = enableDisableMolecules.first else { + return; + } + + var valid = true + for molecule in fieldMolecules { + valid = valid && molecule.isValidField() + } + let enableField = valid && (extraValidationBlock?() ?? true) + enableMolecule.enableField?(enableField) + } + + func enableByGroup() { var groupValue: [String: Bool] = [:] for molecule in fieldMolecules { let valid = molecule.isValidField() @@ -52,10 +75,7 @@ import MVMCore groupValue[grouName] = valid && (groupValue[grouName] ?? true) } } - shouldEnable(groupValue) - } - - public func shouldEnable(_ groupValue: [String: Bool]) { + for molecule in enableDisableMolecules { var valid = false for groupName in molecule.requiredFields?() ?? [] { @@ -64,6 +84,7 @@ import MVMCore molecule.enableField?(valid) } } + }