refactored for validator

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-03-05 14:44:19 -06:00
parent 8b591103c8
commit d847bc3859
2 changed files with 11 additions and 11 deletions

View File

@ -13,7 +13,7 @@ import Combine
/// Base Class used to build out a Input controls.
@objc(VDSEntryField)
open class EntryFieldBase: Control, Changeable, FormFieldable {
open class EntryFieldBase: Control, Changeable, FormFieldValidatable {
//--------------------------------------------------
// MARK: - Initializers
@ -153,8 +153,11 @@ open class EntryFieldBase: Control, Changeable, FormFieldable {
/// Whether not to show the error.
open var showError: Bool = false { didSet { setNeedsUpdate() } }
/// FormFieldValidator
open var validator: (any FormFieldValidatorable)?
/// Whether or not to show the internal error
open internal(set) var hasInternalError: Bool = false { didSet { setNeedsUpdate() } }
open var hasInternalError: Bool { !(validator?.isValid ?? true) }
/// Override UIControl state to add the .error state if showError is true.
open override var state: UIControl.State {
@ -175,7 +178,7 @@ open class EntryFieldBase: Control, Changeable, FormFieldable {
}
}
internal var internalErrorText: String? {
open var internalErrorText: String? {
didSet {
updateContainerView()
updateErrorLabel()
@ -323,6 +326,8 @@ open class EntryFieldBase: Control, Changeable, FormFieldable {
updateHelperLabel()
backgroundColor = surface.color
validator?.validate()
internalErrorText = validator?.errorMessage
}
//--------------------------------------------------

View File

@ -127,6 +127,8 @@ open class TextArea: EntryFieldBase {
$0.isScrollEnabled = false
}
open override var maxLength: Int? { willSet { countRule.maxLength = newValue }}
/// Color configuration for error icon.
internal var iconColorConfiguration = ControlColorConfiguration().with {
$0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forState: .normal)
@ -149,6 +151,7 @@ open class TextArea: EntryFieldBase {
open override func setup() {
super.setup()
accessibilityLabel = "TextArea"
validator = FormFieldValidator<TextArea>(field: self, rules: [.init(countRule)])
containerStackView.pinToSuperView(.uniform(VDSFormControls.spaceInset))
minWidthConstraint = containerView.widthAnchor.constraint(greaterThanOrEqualToConstant: containerSize.width)
@ -189,7 +192,6 @@ open class TextArea: EntryFieldBase {
/// Used to make changes to the View based off a change events or from local properties.
open override func updateView() {
super.updateView()
countRule.maxLength = maxLength
textView.isEditable = isEnabled
textView.isEnabled = isEnabled
textView.surface = surface
@ -211,9 +213,6 @@ open class TextArea: EntryFieldBase {
} else {
characterCounterLabel.text = ""
}
hasInternalError = !validator.validate()
internalErrorText = validator.errorMessage
icon.size = .medium
icon.color = iconColorConfiguration.getColor(self)
@ -281,10 +280,6 @@ open class TextArea: EntryFieldBase {
// MARK: - Validation
//--------------------------------------------------
var countRule = CharacterCountRule()
lazy var validator: FieldValidator<TextArea> = {
let validator = FieldValidator<TextArea>(field: self, rules: [.init(countRule)])
return validator
}()
class CharacterCountRule: Rule {
var maxLength: Int?