Merge branch 'feature/rules_error' into 'develop'
Feature/rules error See merge request BPHV_MIPS/mvm_core_ui!423
This commit is contained in:
commit
243829f30c
@ -67,7 +67,12 @@ import Foundation
|
||||
}
|
||||
|
||||
public func setValidity(_ valid: Bool, rule: RulesProtocol) {
|
||||
if let fieldKey = fieldKey,
|
||||
let ruleErrorMessage = rule.errorMessage?[fieldKey] {
|
||||
self.errorMessage = ruleErrorMessage
|
||||
}
|
||||
self.isValid = valid
|
||||
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
|
||||
@ -244,14 +244,21 @@ import UIKit
|
||||
self.isValid = isValid
|
||||
|
||||
if previousValidity && !isValid {
|
||||
showError = true
|
||||
observingTextFieldDelegate?.isInvalid?(textfield: self)
|
||||
shouldShowError(true)
|
||||
} else if (!previousValidity && isValid) {
|
||||
showError = false
|
||||
observingTextFieldDelegate?.isValid?(textfield: self)
|
||||
shouldShowError(false)
|
||||
}
|
||||
}
|
||||
|
||||
func shouldShowError(_ showError: Bool) {
|
||||
self.showError = showError
|
||||
if showError {
|
||||
observingTextFieldDelegate?.isValid?(textfield: self)
|
||||
entryFieldContainer.originalUI()
|
||||
} else {
|
||||
observingTextFieldDelegate?.isInvalid?(textfield: self)
|
||||
}
|
||||
}
|
||||
|
||||
/// Executes on UITextField.textDidBeginEditingNotification
|
||||
@objc func startEditing() {
|
||||
isSelected = true
|
||||
@ -268,10 +275,16 @@ import UIKit
|
||||
/// Executes on UITextField.textDidEndEditingNotification
|
||||
@objc func endInputing() {
|
||||
resignFirstResponder()
|
||||
if isValid {
|
||||
showError = false
|
||||
entryFieldContainer.bottomBar?.backgroundColor = UIColor.mvmBlack.cgColor
|
||||
|
||||
// Don't show error till user starts typing.
|
||||
guard text?.count ?? 0 != 0 else {
|
||||
return
|
||||
}
|
||||
|
||||
if let isValid = (model as? TextEntryFieldModel)?.isValid {
|
||||
self.isValid = isValid
|
||||
}
|
||||
shouldShowError(!isValid)
|
||||
}
|
||||
|
||||
@objc public func dismissFieldInput(_ sender: Any?) {
|
||||
|
||||
@ -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,9 +28,9 @@ public class RuleEqualsModel: RulesProtocol {
|
||||
}
|
||||
|
||||
public func validate(_ fieldMolecules: [String: FormFieldProtocol]) -> Bool {
|
||||
var valid = true
|
||||
var compareValue: AnyHashable?
|
||||
|
||||
var valid = true
|
||||
var compareValue: AnyHashable?
|
||||
|
||||
for formKey in fields {
|
||||
guard let formField = fieldMolecules[formKey] else { continue }
|
||||
|
||||
@ -37,13 +38,16 @@ public class RuleEqualsModel: RulesProtocol {
|
||||
compareValue = formField.formFieldValue()
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
if compareValue != formField.formFieldValue() {
|
||||
valid = false
|
||||
(formField as? FormRuleWatcherFieldProtocol)?.setValidity(valid, rule: self)
|
||||
break
|
||||
} else {
|
||||
(formField as? FormRuleWatcherFieldProtocol)?.setValidity(valid, rule: self)
|
||||
}
|
||||
}
|
||||
|
||||
return valid
|
||||
|
||||
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 }
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user