any required fixes

This commit is contained in:
Suresh, Kamlesh 2020-04-10 17:35:46 -04:00
parent b1caad0928
commit c665f547a6
4 changed files with 17 additions and 16 deletions

View File

@ -88,7 +88,7 @@ import MVMCore
// Validate each rule. // Validate each rule.
var valid = true var valid = true
for rule in group.rules { for rule in group.rules {
valid = valid && validateRule(rule) valid = valid && rule.validate(fields)
} }
// Notify the group watchers of validity. // Notify the group watchers of validity.
@ -97,19 +97,6 @@ import MVMCore
watcher.setValidity(valid, group: group) watcher.setValidity(valid, group: group)
} }
} }
return valid
}
/// Validates a given rule. Returns if valid.
public func validateRule(_ rule: RulesProtocol) -> Bool {
var valid = true
for formKey in rule.fields {
guard let formField = fields[formKey] else { continue }
let fieldValidity = rule.isValid(formField)
(formField as? FormRuleWatcherFieldProtocol)?.setValidity(fieldValidity, rule: rule)
valid = valid && fieldValidity
}
return valid return valid
} }
} }

View File

@ -35,7 +35,7 @@ public class RuleAnyRequiredModel: RulesProtocol {
return false return false
} }
public func isValid(_ fieldMolecules: [String: FormFieldProtocol]) -> Bool { public func validate(_ fieldMolecules: [String: FormFieldProtocol]) -> Bool {
for formKey in fields { for formKey in fields {
guard let formField = fieldMolecules[formKey] else { continue } guard let formField = fieldMolecules[formKey] else { continue }

View File

@ -18,7 +18,7 @@ public class RuleEqualsModel: RulesProtocol {
return false return false
} }
public func isValid(_ fieldMolecules: [String: FormFieldProtocol]) -> Bool { public func validate(_ fieldMolecules: [String: FormFieldProtocol]) -> Bool {
var valid = true var valid = true
var compareValue: AnyHashable? var compareValue: AnyHashable?
for formKey in fields { for formKey in fields {

View File

@ -22,6 +22,9 @@ public protocol RulesProtocol: ModelProtocol {
// Returns if a given field is valid according to the rule // Returns if a given field is valid according to the rule
func isValid(_ formField: FormFieldProtocol) -> Bool func isValid(_ formField: FormFieldProtocol) -> Bool
// Returns if a the rule is valid
func validate(_ fieldMolecules: [String: FormFieldProtocol]) -> Bool
} }
public extension RulesProtocol { public extension RulesProtocol {
@ -37,4 +40,15 @@ public extension RulesProtocol {
static var categoryName: String { static var categoryName: String {
return "\(RulesProtocol.self)" return "\(RulesProtocol.self)"
} }
func validate(_ fieldMolecules: [String: FormFieldProtocol]) -> Bool {
var valid = true
for formKey in fields {
guard let formField = fieldMolecules[formKey] else { continue }
let fieldValidity = isValid(formField)
(formField as? FormRuleWatcherFieldProtocol)?.setValidity(fieldValidity, rule: self)
valid = valid && fieldValidity
}
return valid
}
} }