rules error handling
This commit is contained in:
parent
89c6204003
commit
33c1c3f70e
@ -67,7 +67,10 @@ import Foundation
|
||||
}
|
||||
|
||||
public func setValidity(_ valid: Bool, rule: RulesProtocol) {
|
||||
self.isValid = valid
|
||||
if let fieldKey = fieldKey {
|
||||
self.errorMessage = rule.errorMessage?[fieldKey]
|
||||
self.isValid = valid
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
|
||||
@ -15,6 +15,7 @@ public class RuleAllValueChangedModel: RulesProtocol {
|
||||
|
||||
public static var identifier: String = "allValueChanged"
|
||||
public var type: String = RuleAllValueChangedModel.identifier
|
||||
public var errorMessage: [String: String]?
|
||||
public var fields: [String]
|
||||
|
||||
//--------------------------------------------------
|
||||
|
||||
@ -17,6 +17,7 @@ public class RuleAnyRequiredModel: RulesProtocol {
|
||||
public static var identifier: String = "anyRequired"
|
||||
public var type: String = RuleRequiredModel.identifier
|
||||
public var fields: [String]
|
||||
public var errorMessage: [String: String]?
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Methods
|
||||
|
||||
@ -16,6 +16,7 @@ public class RuleAnyValueChangedModel: RulesProtocol {
|
||||
|
||||
public static var identifier: String = "anyValueChanged"
|
||||
public var type: String = RuleAnyValueChangedModel.identifier
|
||||
public var errorMessage: [String: String]?
|
||||
public var fields: [String]
|
||||
|
||||
//--------------------------------------------------
|
||||
|
||||
@ -17,6 +17,7 @@ public class RuleEqualsModel: RulesProtocol {
|
||||
public static var identifier: String = "equals"
|
||||
public var type: String = RuleEqualsModel.identifier
|
||||
public var fields: [String]
|
||||
public var errorMessage: [String: String]?
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Validation
|
||||
@ -27,23 +28,24 @@ public class RuleEqualsModel: RulesProtocol {
|
||||
}
|
||||
|
||||
public func validate(_ fieldMolecules: [String: FormFieldProtocol]) -> Bool {
|
||||
var valid = true
|
||||
var compareValue: AnyHashable?
|
||||
|
||||
for formKey in fields {
|
||||
guard let formField = fieldMolecules[formKey] else { continue }
|
||||
var valid = true
|
||||
var compareValue: AnyHashable?
|
||||
|
||||
if compareValue == nil {
|
||||
compareValue = formField.formFieldValue()
|
||||
continue
|
||||
}
|
||||
|
||||
if compareValue != formField.formFieldValue() {
|
||||
valid = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return valid
|
||||
for formKey in fields {
|
||||
guard let formField = fieldMolecules[formKey] else { continue }
|
||||
|
||||
if compareValue == nil {
|
||||
compareValue = formField.formFieldValue()
|
||||
continue
|
||||
}
|
||||
|
||||
if compareValue != formField.formFieldValue() {
|
||||
valid = false
|
||||
(formField as? FormRuleWatcherFieldProtocol)?.setValidity(valid, rule: self)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return valid
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,6 +18,7 @@ public class RuleRegexModel: RulesProtocol {
|
||||
public var type: String = RuleRegexModel.identifier
|
||||
public var fields: [String]
|
||||
public var regex: String
|
||||
public var errorMessage: [String: String]?
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Properties
|
||||
|
||||
@ -16,6 +16,7 @@ public class RuleRequiredModel: RulesProtocol {
|
||||
|
||||
public static var identifier: String = "allRequired"
|
||||
public var type: String = RuleRequiredModel.identifier
|
||||
public var errorMessage: [String: String]?
|
||||
public var fields: [String]
|
||||
|
||||
//--------------------------------------------------
|
||||
|
||||
@ -16,7 +16,9 @@ public enum RulesCodingKey: String, CodingKey {
|
||||
public protocol RulesProtocol: ModelProtocol {
|
||||
// The type of rule
|
||||
var type: String { get }
|
||||
|
||||
|
||||
var errorMessage: [String: String]? { get }
|
||||
|
||||
// The fields that this rule applies to.
|
||||
var fields: [String] { get set }
|
||||
|
||||
@ -33,6 +35,10 @@ public extension RulesProtocol {
|
||||
get { return Self.identifier }
|
||||
}
|
||||
|
||||
// var errorMessage: String? {
|
||||
// return nil
|
||||
// }
|
||||
|
||||
static var categoryCodingKey: String {
|
||||
return "type"
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user