rules error handling

This commit is contained in:
Suresh, Kamlesh 2020-05-06 12:16:33 -04:00
parent 89c6204003
commit 33c1c3f70e
8 changed files with 35 additions and 19 deletions

View File

@ -67,8 +67,11 @@ import Foundation
} }
public func setValidity(_ valid: Bool, rule: RulesProtocol) { public func setValidity(_ valid: Bool, rule: RulesProtocol) {
if let fieldKey = fieldKey {
self.errorMessage = rule.errorMessage?[fieldKey]
self.isValid = valid self.isValid = valid
} }
}
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Initializers // MARK: - Initializers

View File

@ -15,6 +15,7 @@ public class RuleAllValueChangedModel: RulesProtocol {
public static var identifier: String = "allValueChanged" public static var identifier: String = "allValueChanged"
public var type: String = RuleAllValueChangedModel.identifier public var type: String = RuleAllValueChangedModel.identifier
public var errorMessage: [String: String]?
public var fields: [String] public var fields: [String]
//-------------------------------------------------- //--------------------------------------------------

View File

@ -17,6 +17,7 @@ public class RuleAnyRequiredModel: RulesProtocol {
public static var identifier: String = "anyRequired" public static var identifier: String = "anyRequired"
public var type: String = RuleRequiredModel.identifier public var type: String = RuleRequiredModel.identifier
public var fields: [String] public var fields: [String]
public var errorMessage: [String: String]?
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Methods // MARK: - Methods

View File

@ -16,6 +16,7 @@ public class RuleAnyValueChangedModel: RulesProtocol {
public static var identifier: String = "anyValueChanged" public static var identifier: String = "anyValueChanged"
public var type: String = RuleAnyValueChangedModel.identifier public var type: String = RuleAnyValueChangedModel.identifier
public var errorMessage: [String: String]?
public var fields: [String] public var fields: [String]
//-------------------------------------------------- //--------------------------------------------------

View File

@ -17,6 +17,7 @@ public class RuleEqualsModel: RulesProtocol {
public static var identifier: String = "equals" public static var identifier: String = "equals"
public var type: String = RuleEqualsModel.identifier public var type: String = RuleEqualsModel.identifier
public var fields: [String] public var fields: [String]
public var errorMessage: [String: String]?
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Validation // MARK: - Validation
@ -40,6 +41,7 @@ public class RuleEqualsModel: RulesProtocol {
if compareValue != formField.formFieldValue() { if compareValue != formField.formFieldValue() {
valid = false valid = false
(formField as? FormRuleWatcherFieldProtocol)?.setValidity(valid, rule: self)
break break
} }
} }

View File

@ -18,6 +18,7 @@ public class RuleRegexModel: RulesProtocol {
public var type: String = RuleRegexModel.identifier public var type: String = RuleRegexModel.identifier
public var fields: [String] public var fields: [String]
public var regex: String public var regex: String
public var errorMessage: [String: String]?
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Properties // MARK: - Properties

View File

@ -16,6 +16,7 @@ public class RuleRequiredModel: RulesProtocol {
public static var identifier: String = "allRequired" public static var identifier: String = "allRequired"
public var type: String = RuleRequiredModel.identifier public var type: String = RuleRequiredModel.identifier
public var errorMessage: [String: String]?
public var fields: [String] public var fields: [String]
//-------------------------------------------------- //--------------------------------------------------

View File

@ -17,6 +17,8 @@ public protocol RulesProtocol: ModelProtocol {
// The type of rule // The type of rule
var type: String { get } var type: String { get }
var errorMessage: [String: String]? { get }
// The fields that this rule applies to. // The fields that this rule applies to.
var fields: [String] { get set } var fields: [String] { get set }
@ -33,6 +35,10 @@ public extension RulesProtocol {
get { return Self.identifier } get { return Self.identifier }
} }
// var errorMessage: String? {
// return nil
// }
static var categoryCodingKey: String { static var categoryCodingKey: String {
return "type" return "type"
} }