From 69a23745040dfb10fe755144124a6905faa455ea Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Thu, 29 Feb 2024 10:34:10 -0600 Subject: [PATCH] First cut at form fiedable change Signed-off-by: Matt Bruce --- .../Attributes/LabelAttributeModel.swift | 2 +- .../TextFields/EntryFieldBase.swift | 4 +- .../TextFields/InputField/InputField.swift | 4 +- .../TextFields/TextArea/TextArea.swift | 43 +++++++++++--- VDS/Protocols/FormFieldable.swift | 58 ++++++++++++++++++- 5 files changed, 97 insertions(+), 14 deletions(-) diff --git a/VDS/Components/Label/Attributes/LabelAttributeModel.swift b/VDS/Components/Label/Attributes/LabelAttributeModel.swift index 87708ffb..ac43472f 100644 --- a/VDS/Components/Label/Attributes/LabelAttributeModel.swift +++ b/VDS/Components/Label/Attributes/LabelAttributeModel.swift @@ -31,7 +31,7 @@ extension LabelAttributeModel { } public func isValidRange(on attributedString: NSMutableAttributedString) -> Bool { - true//range.location + range.length <= attributedString.string.count + range.location + range.length <= attributedString.string.count } } diff --git a/VDS/Components/TextFields/EntryFieldBase.swift b/VDS/Components/TextFields/EntryFieldBase.swift index e66d31a5..3b738549 100644 --- a/VDS/Components/TextFields/EntryFieldBase.swift +++ b/VDS/Components/TextFields/EntryFieldBase.swift @@ -13,7 +13,7 @@ import Combine /// Base Class used to build out a Input controls. @objc(VDSEntryField) -open class EntryFieldBase: Control, Changeable { +open class EntryFieldBase: Control, Changeable, FormFieldable { //-------------------------------------------------- // MARK: - Initializers @@ -196,7 +196,7 @@ open class EntryFieldBase: Control, Changeable { open var inputId: String? { didSet { setNeedsUpdate() } } - open var value: AnyHashable? { didSet { setNeedsUpdate() } } + open var value: String? { didSet { setNeedsUpdate() } } open var defaultValue: AnyHashable? { didSet { setNeedsUpdate() } } diff --git a/VDS/Components/TextFields/InputField/InputField.swift b/VDS/Components/TextFields/InputField/InputField.swift index 589ba28f..7a950a80 100644 --- a/VDS/Components/TextFields/InputField/InputField.swift +++ b/VDS/Components/TextFields/InputField/InputField.swift @@ -79,13 +79,13 @@ open class InputField: EntryFieldBase, UITextFieldDelegate { open var fieldType: FieldType = .text { didSet { setNeedsUpdate() } } /// The text of this textField. - open override var text: String? { + open override var value: String? { get { textField.text } set { textField.text = newValue } } - + var _showError: Bool = false /// Whether not to show the error. open override var showError: Bool { diff --git a/VDS/Components/TextFields/TextArea/TextArea.swift b/VDS/Components/TextFields/TextArea/TextArea.swift index 92f5525a..4b99eeb3 100644 --- a/VDS/Components/TextFields/TextArea/TextArea.swift +++ b/VDS/Components/TextFields/TextArea/TextArea.swift @@ -107,14 +107,15 @@ open class TextArea: EntryFieldBase { } } - /// The text of this textField. - open override var text: String? { + open override var value: String? { get { textView.text } set { textView.text = newValue + setNeedsUpdate() + } } - + /// UITextView shown in the TextArea. open var textView = TextView().with { $0.translatesAutoresizingMaskIntoConstraints = false @@ -184,7 +185,7 @@ 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 @@ -208,6 +209,9 @@ open class TextArea: EntryFieldBase { } else { characterCounterLabel.text = "" } + + showError = !validator.validate() + errorText = validator.errorMessage icon.size = .medium icon.color = iconColorConfiguration.getColor(self) @@ -246,12 +250,8 @@ open class TextArea: EntryFieldBase { let countStr = (count > maxLength ?? 0) ? ("-" + "\(count-(maxLength ?? 0))") : "\(count)" if ((maxLength ?? 0) > 0) { if (count > (maxLength ?? 0)) { - showError = true - errorText = "You have exceeded the character limit." return countStr } else { - showError = false - errorText = "" return ("\(countStr)" + "/" + "\(maxLength ?? 0)") } } else { @@ -274,6 +274,33 @@ open class TextArea: EntryFieldBase { textView.textAttributes = textAttributes } + + //-------------------------------------------------- + // MARK: - Validation + //-------------------------------------------------- + var countRule = CharacterCountRule() + lazy var validator: FieldValidator