refactored more code

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-04-30 16:38:40 -05:00
parent c0b59694f2
commit 65ce18d083
2 changed files with 24 additions and 18 deletions

View File

@ -160,13 +160,7 @@ open class EntryFieldBase: Control, Changeable, FormFieldInternalValidatable {
open var showError: Bool = false { didSet { setNeedsUpdate() } }
/// FormFieldValidator
internal var validator: (any FormFieldValidatorable)?
/// Whether or not to show the internal error
open var hasInternalError: Bool {
guard let validator, !showError else { return false }
return !validator.isValid
}
open var validator: (any FormFieldValidatorable)?
/// Override UIControl state to add the .error state if showError is true.
open override var state: UIControl.State {
@ -180,16 +174,7 @@ open class EntryFieldBase: Control, Changeable, FormFieldInternalValidatable {
}
open var errorText: String? { didSet { setNeedsUpdate() } }
open var internalErrorText: String? {
guard let validator, !validator.isValid else { return nil }
if let errorText, !errorText.isEmpty {
return errorText
} else {
return validator.errorMessage
}
}
open var tooltipModel: Tooltip.TooltipModel? { didSet { setNeedsUpdate() } }
open var transparentBackground: Bool = false { didSet { setNeedsUpdate() } }

View File

@ -19,11 +19,32 @@ public protocol FormFieldable {
}
/// Protocol for FormFieldable that require internal validation.
public protocol FormFieldInternalValidatable: FormFieldable {
public protocol FormFieldInternalValidatable: FormFieldable, Errorable {
/// Is there an internalError
var hasInternalError: Bool { get }
/// Internal Error Message that will show.
var internalErrorText: String? { get }
var validator: (any FormFieldValidatorable)? { get set }
func validate()
}
extension FormFieldInternalValidatable {
/// Whether or not to show the internal error
public var hasInternalError: Bool {
guard let validator, !showError else { return false }
return !validator.isValid
}
public var internalErrorText: String? {
guard let validator, !validator.isValid else { return nil }
if let errorText, !errorText.isEmpty {
return errorText
} else {
return validator.errorMessage
}
}
}
/// Struct that will execute the validation.