refactored for validator
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
8b591103c8
commit
d847bc3859
@ -13,7 +13,7 @@ import Combine
|
|||||||
|
|
||||||
/// Base Class used to build out a Input controls.
|
/// Base Class used to build out a Input controls.
|
||||||
@objc(VDSEntryField)
|
@objc(VDSEntryField)
|
||||||
open class EntryFieldBase: Control, Changeable, FormFieldable {
|
open class EntryFieldBase: Control, Changeable, FormFieldValidatable {
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Initializers
|
// MARK: - Initializers
|
||||||
@ -153,8 +153,11 @@ open class EntryFieldBase: Control, Changeable, FormFieldable {
|
|||||||
/// Whether not to show the error.
|
/// Whether not to show the error.
|
||||||
open var showError: Bool = false { didSet { setNeedsUpdate() } }
|
open var showError: Bool = false { didSet { setNeedsUpdate() } }
|
||||||
|
|
||||||
|
/// FormFieldValidator
|
||||||
|
open var validator: (any FormFieldValidatorable)?
|
||||||
|
|
||||||
/// Whether or not to show the internal error
|
/// 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.
|
/// Override UIControl state to add the .error state if showError is true.
|
||||||
open override var state: UIControl.State {
|
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 {
|
didSet {
|
||||||
updateContainerView()
|
updateContainerView()
|
||||||
updateErrorLabel()
|
updateErrorLabel()
|
||||||
@ -323,6 +326,8 @@ open class EntryFieldBase: Control, Changeable, FormFieldable {
|
|||||||
updateHelperLabel()
|
updateHelperLabel()
|
||||||
|
|
||||||
backgroundColor = surface.color
|
backgroundColor = surface.color
|
||||||
|
validator?.validate()
|
||||||
|
internalErrorText = validator?.errorMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
|
|||||||
@ -127,6 +127,8 @@ open class TextArea: EntryFieldBase {
|
|||||||
$0.isScrollEnabled = false
|
$0.isScrollEnabled = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
open override var maxLength: Int? { willSet { countRule.maxLength = newValue }}
|
||||||
|
|
||||||
/// Color configuration for error icon.
|
/// Color configuration for error icon.
|
||||||
internal var iconColorConfiguration = ControlColorConfiguration().with {
|
internal var iconColorConfiguration = ControlColorConfiguration().with {
|
||||||
$0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forState: .normal)
|
$0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forState: .normal)
|
||||||
@ -149,6 +151,7 @@ open class TextArea: EntryFieldBase {
|
|||||||
open override func setup() {
|
open override func setup() {
|
||||||
super.setup()
|
super.setup()
|
||||||
accessibilityLabel = "TextArea"
|
accessibilityLabel = "TextArea"
|
||||||
|
validator = FormFieldValidator<TextArea>(field: self, rules: [.init(countRule)])
|
||||||
|
|
||||||
containerStackView.pinToSuperView(.uniform(VDSFormControls.spaceInset))
|
containerStackView.pinToSuperView(.uniform(VDSFormControls.spaceInset))
|
||||||
minWidthConstraint = containerView.widthAnchor.constraint(greaterThanOrEqualToConstant: containerSize.width)
|
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.
|
/// Used to make changes to the View based off a change events or from local properties.
|
||||||
open override func updateView() {
|
open override func updateView() {
|
||||||
super.updateView()
|
super.updateView()
|
||||||
countRule.maxLength = maxLength
|
|
||||||
textView.isEditable = isEnabled
|
textView.isEditable = isEnabled
|
||||||
textView.isEnabled = isEnabled
|
textView.isEnabled = isEnabled
|
||||||
textView.surface = surface
|
textView.surface = surface
|
||||||
@ -211,9 +213,6 @@ open class TextArea: EntryFieldBase {
|
|||||||
} else {
|
} else {
|
||||||
characterCounterLabel.text = ""
|
characterCounterLabel.text = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
hasInternalError = !validator.validate()
|
|
||||||
internalErrorText = validator.errorMessage
|
|
||||||
|
|
||||||
icon.size = .medium
|
icon.size = .medium
|
||||||
icon.color = iconColorConfiguration.getColor(self)
|
icon.color = iconColorConfiguration.getColor(self)
|
||||||
@ -281,10 +280,6 @@ open class TextArea: EntryFieldBase {
|
|||||||
// MARK: - Validation
|
// MARK: - Validation
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
var countRule = CharacterCountRule()
|
var countRule = CharacterCountRule()
|
||||||
lazy var validator: FieldValidator<TextArea> = {
|
|
||||||
let validator = FieldValidator<TextArea>(field: self, rules: [.init(countRule)])
|
|
||||||
return validator
|
|
||||||
}()
|
|
||||||
|
|
||||||
class CharacterCountRule: Rule {
|
class CharacterCountRule: Rule {
|
||||||
var maxLength: Int?
|
var maxLength: Int?
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user