Merge branch 'feature/vds-form-controls' into feature/atomic-vds-textArea
This commit is contained in:
commit
80edb943b2
@ -578,6 +578,7 @@
|
||||
EA17584A2BC97EF100A5C0D9 /* BadgeIndicatorModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA1758492BC97EF100A5C0D9 /* BadgeIndicatorModel.swift */; };
|
||||
EA17584C2BC9894800A5C0D9 /* ButtonIconModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA17584B2BC9894800A5C0D9 /* ButtonIconModel.swift */; };
|
||||
EA17584E2BC9895A00A5C0D9 /* ButtonIcon.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA17584D2BC9895A00A5C0D9 /* ButtonIcon.swift */; };
|
||||
EA1B02DE2C41BFD200F0758B /* RuleVDSModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA1B02DD2C41BFD200F0758B /* RuleVDSModel.swift */; };
|
||||
EA41F4AC2787927100F5B377 /* DynamicRuleFormFieldEffectModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA41F4AB2787927100F5B377 /* DynamicRuleFormFieldEffectModel.swift */; };
|
||||
EA5124FD243601600051A3A4 /* BGImageHeadlineBodyButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA5124FC243601600051A3A4 /* BGImageHeadlineBodyButton.swift */; };
|
||||
EA5124FF2436018E0051A3A4 /* BGImageHeadlineBodyButtonModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA5124FE2436018E0051A3A4 /* BGImageHeadlineBodyButtonModel.swift */; };
|
||||
@ -1199,6 +1200,7 @@
|
||||
EA1758492BC97EF100A5C0D9 /* BadgeIndicatorModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BadgeIndicatorModel.swift; sourceTree = "<group>"; };
|
||||
EA17584B2BC9894800A5C0D9 /* ButtonIconModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonIconModel.swift; sourceTree = "<group>"; };
|
||||
EA17584D2BC9895A00A5C0D9 /* ButtonIcon.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonIcon.swift; sourceTree = "<group>"; };
|
||||
EA1B02DD2C41BFD200F0758B /* RuleVDSModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RuleVDSModel.swift; sourceTree = "<group>"; };
|
||||
EA41F4AB2787927100F5B377 /* DynamicRuleFormFieldEffectModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DynamicRuleFormFieldEffectModel.swift; sourceTree = "<group>"; };
|
||||
EA5124FC243601600051A3A4 /* BGImageHeadlineBodyButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BGImageHeadlineBodyButton.swift; sourceTree = "<group>"; };
|
||||
EA5124FE2436018E0051A3A4 /* BGImageHeadlineBodyButtonModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BGImageHeadlineBodyButtonModel.swift; sourceTree = "<group>"; };
|
||||
@ -1296,6 +1298,7 @@
|
||||
011D95A0240453D0000E3791 /* RuleEqualsModel.swift */,
|
||||
0A849EFD246F1775009F277F /* RuleEqualsIgnoreCaseModel.swift */,
|
||||
FD9912FF28E21E4900542CC3 /* RuleNotEqualsModel.swift */,
|
||||
EA1B02DD2C41BFD200F0758B /* RuleVDSModel.swift */,
|
||||
);
|
||||
name = Rules;
|
||||
path = Rules/Rules;
|
||||
@ -2836,6 +2839,7 @@
|
||||
D253BB8A24574CC5002DE544 /* StackModel.swift in Sources */,
|
||||
EAB14BC127D935F00012AB2C /* RuleCompareModelProtocol.swift in Sources */,
|
||||
011D95A924057AC7000E3791 /* FormGroupWatcherFieldProtocol.swift in Sources */,
|
||||
EA1B02DE2C41BFD200F0758B /* RuleVDSModel.swift in Sources */,
|
||||
EA985C892981AB7100F2FF2E /* VDS-TextStyle.swift in Sources */,
|
||||
BB2BF0EA2452A9BB001D0FC2 /* ListDeviceComplexButtonSmall.swift in Sources */,
|
||||
D20C700B250BFDE40095B21C /* NotificationContainerView.swift in Sources */,
|
||||
|
||||
@ -25,6 +25,7 @@ import VDS
|
||||
public var readOnly: Bool = false
|
||||
public var showError: Bool?
|
||||
public var errorMessage: String?
|
||||
public var initialErrorMessage: String?
|
||||
|
||||
public var fieldKey: String?
|
||||
public var groupName: String = FormValidator.defaultGroupName
|
||||
@ -89,9 +90,11 @@ import VDS
|
||||
|
||||
if let ruleErrorMessage = errorMessage, fieldKey != nil {
|
||||
self.errorMessage = ruleErrorMessage
|
||||
} else {
|
||||
self.errorMessage = initialErrorMessage
|
||||
}
|
||||
|
||||
self.isValid = valid
|
||||
isValid = valid
|
||||
updateUI?()
|
||||
}
|
||||
|
||||
@ -103,6 +106,7 @@ import VDS
|
||||
id = try typeContainer.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
|
||||
accessibilityIdentifier = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityIdentifier)
|
||||
errorMessage = try typeContainer.decodeIfPresent(String.self, forKey: .errorMessage)
|
||||
initialErrorMessage = errorMessage
|
||||
enabled = try typeContainer.decodeIfPresent(Bool.self, forKey: .enabled) ?? true
|
||||
required = try typeContainer.decodeIfPresent(Bool.self, forKey: .required) ?? true
|
||||
readOnly = try typeContainer.decodeIfPresent(Bool.self, forKey: .readOnly) ?? false
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
// Copyright © 2020 Verizon Wireless. All rights reserved.
|
||||
//
|
||||
// Form fields are items can be interacted with. They have value, and may need to be validated.
|
||||
|
||||
import VDS
|
||||
|
||||
public protocol FormFieldProtocol: FormItemProtocol {
|
||||
|
||||
@ -36,6 +36,21 @@ public extension FormFieldProtocol {
|
||||
}
|
||||
}
|
||||
|
||||
public protocol FormFieldInternalValidatableProtocol: FormFieldProtocol {
|
||||
associatedtype ValueType
|
||||
var rules: [AnyRule<ValueType>]? { get set }
|
||||
var internalRules: [RuleAnyModelProtocol]? { get }
|
||||
}
|
||||
|
||||
extension FormFieldInternalValidatableProtocol {
|
||||
public var internalRules: [RuleAnyModelProtocol]? {
|
||||
guard let fieldKey else { return nil }
|
||||
return rules?.compactMap{ rule in
|
||||
return RuleVDSModel(field: fieldKey, rule: rule)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class FormFieldValidity{
|
||||
public var fieldKey: String
|
||||
public var valid: Bool = true
|
||||
|
||||
@ -44,6 +44,29 @@ import MVMCore
|
||||
if let fieldKey = field.fieldKey {
|
||||
fields[fieldKey] = field
|
||||
}
|
||||
// add internal validators if needed
|
||||
if let field = field as? any FormFieldInternalValidatableProtocol {
|
||||
addInternalRules(field)
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds additional Rules that are from another source
|
||||
private func addInternalRules(_ field: any FormFieldInternalValidatableProtocol) {
|
||||
if let internalRules = field.internalRules, !internalRules.isEmpty {
|
||||
|
||||
//find the group
|
||||
if let formGroup = formRules?.first(where: {$0.groupName == field.groupName}) {
|
||||
formGroup.rules.append(contentsOf: internalRules)
|
||||
} else {
|
||||
//create the new group
|
||||
let formGroup = FormGroupRule(field.groupName, internalRules, [])
|
||||
if var formRules {
|
||||
formRules.append(formGroup)
|
||||
} else {
|
||||
formRules = [formGroup]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds the form action to the validator.
|
||||
@ -72,7 +95,6 @@ import MVMCore
|
||||
if let validator = delegate?.formValidator {
|
||||
validator.delegate = delegate
|
||||
validator.insert(item)
|
||||
|
||||
// TODO: Temporary hacks, rewrite architecture to support this.
|
||||
_ = validator.validate()
|
||||
}
|
||||
|
||||
59
MVMCoreUI/FormUIHelpers/Rules/Rules/RuleVDSModel.swift
Normal file
59
MVMCoreUI/FormUIHelpers/Rules/Rules/RuleVDSModel.swift
Normal file
@ -0,0 +1,59 @@
|
||||
//
|
||||
// RuleVDSModel.swift
|
||||
// MVMCoreUI
|
||||
//
|
||||
// Created by Matt Bruce on 7/12/24.
|
||||
// Copyright © 2024 Verizon Wireless. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import VDS
|
||||
|
||||
open class VDSRuleBase: RuleAnyModelProtocol {
|
||||
open var ruleId: String?
|
||||
open var errorMessage: [String : String]?
|
||||
open var fields = [String]()
|
||||
public init(){}
|
||||
|
||||
open func isValid(_ formField: any FormFieldProtocol) -> Bool {
|
||||
fatalError()
|
||||
}
|
||||
public static var identifier: String = "AnyVDSRule"
|
||||
}
|
||||
|
||||
public class RuleVDSModel<ValueType>: VDSRuleBase {
|
||||
//--------------------------------------------------
|
||||
// MARK: - Properties
|
||||
//--------------------------------------------------
|
||||
public var rule: AnyRule<ValueType>
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Initializer
|
||||
//--------------------------------------------------
|
||||
|
||||
public init(field: String, rule: AnyRule<ValueType>) {
|
||||
self.rule = rule
|
||||
super.init()
|
||||
self.fields = [field]
|
||||
self.ruleId = "\(rule.self)-\(Int.random(in: 1...1000))"
|
||||
}
|
||||
|
||||
required init(from decoder: any Decoder) throws {
|
||||
fatalError("init(from:) has not been implemented")
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Validation
|
||||
//--------------------------------------------------
|
||||
|
||||
public override func isValid(_ formField: FormFieldProtocol) -> Bool {
|
||||
let value = formField.formFieldValue() as? ValueType
|
||||
let valid = rule.isValid(value: value)
|
||||
if let field = fields.first, valid {
|
||||
errorMessage = [field: rule.errorMessage]
|
||||
} else {
|
||||
errorMessage = nil
|
||||
}
|
||||
return valid
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user