Merge branch 'bugfix/form_validation' into 'develop'

any required fixes

See merge request BPHV_MIPS/mvm_core_ui!372
This commit is contained in:
Pfeil, Scott Robert 2020-04-13 13:33:06 -04:00
commit d000715518
5 changed files with 20 additions and 22 deletions

View File

@ -89,7 +89,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.
@ -98,19 +98,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

@ -26,16 +26,13 @@ public class RuleAnyValueChangedModel: RulesProtocol {
return formField.baseValue != formField.formFieldValue() return formField.baseValue != formField.formFieldValue()
} }
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 }
if isValid(formField) { if isValid(formField) {
return true return true
} }
} }
return false return false
} }
} }

View File

@ -26,8 +26,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?

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
// Validates the rule and returns the result.
func validate(_ fieldMolecules: [String: FormFieldProtocol]) -> Bool
} }
public extension RulesProtocol { public extension RulesProtocol {
@ -37,4 +40,16 @@ public extension RulesProtocol {
static var categoryName: String { static var categoryName: String {
return "\(RulesProtocol.self)" return "\(RulesProtocol.self)"
} }
// Individual rule can override the function to validate based on the rule type.
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
}
} }