improvements

This commit is contained in:
Suresh, Kamlesh 2019-09-13 15:25:13 -04:00
parent eac6bf3aa3
commit daed451c7a
3 changed files with 31 additions and 36 deletions

View File

@ -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];

View File

@ -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

View File

@ -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)
}
}
}