Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue with delegate call. Added ability to set custom error texts. #42

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Source/FormTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ open class FormTextField: UITextField, UITextFieldDelegate {

// Sets the textfields at the initial state, clear text and resets appearance too
open func reset() {
inputValidator?.validation?.errors.reset()
updateText(nil)
updateEnabled(isEnabled)
}
Expand Down Expand Up @@ -194,6 +195,7 @@ open class FormTextField: UITextField, UITextFieldDelegate {

open func validate(updatingUI: Bool = true) -> Bool {
var isValid = true
inputValidator?.validation?.errors.reset()
if let inputValidator = self.inputValidator {
isValid = inputValidator.validateString(text ?? "")
}
Expand Down Expand Up @@ -243,6 +245,8 @@ extension FormTextField {
}

updateActive(true)

textFieldDelegate?.formTextFieldDidBeginEditing?(self)
}

public func textFieldDidEndEditing(_: UITextField) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public struct CardExpirationDateInputValidator: InputValidatable {
public var validation: Validation?

public init(validation _: Validation? = nil) {
var predefinedValidation = Validation()
var predefinedValidation = Validation(name: "Card expiration date")
predefinedValidation.minimumLength = "MM/YY".count
predefinedValidation.maximumLength = "MM/YY".count
// predefinedValidation.required = validation?.required ?? false
Expand Down
141 changes: 118 additions & 23 deletions Source/Validation/Validation.swift
Original file line number Diff line number Diff line change
@@ -1,59 +1,154 @@
import Foundation

public struct Validation {
public var minimumLength = 0
public var maximumLength: Int?
public var maximumValue: Double?
public var minimumValue: Double?
public var characterSet: CharacterSet?
public var format: String?
public class ValidationErrors: CustomStringConvertible {

public typealias ValidationError = (text: String?, occured: Bool)

public var minimumLength = ValidationError(nil, false)
public var maximumLength = ValidationError(nil, false)
public var minimumValue = ValidationError(nil, false)
public var maximumValue = ValidationError(nil, false)
public var characterSet = ValidationError(nil, false)
public var format = ValidationError(nil, false)

public func reset() {
minimumLength.occured = false
maximumLength.occured = false
minimumValue.occured = false
maximumValue.occured = false
characterSet.occured = false
format.occured = false
}

// MARK: - CustomStringConvertible

public var description: String {
var string = ""
let list: [ValidationError] = [minimumLength, maximumLength, minimumValue, maximumValue, characterSet, format]
for item in list {
if let text = item.text, item.occured {
string.append(" \(text).")
}
}
// remove whitespaces and possible repeating dots
return string.trimmingCharacters(in: .whitespaces).replacingOccurrences(of: "..", with: ".")
}

}

public init() {}
public class Validation {

public var name: String? { didSet { reloadErrorTexts() } }
public var minimumLength = 0 { didSet { reloadErrorTexts() } }
public var maximumLength: Int? { didSet { reloadErrorTexts() } }
public var maximumValue: Double? { didSet { reloadErrorTexts() } }
public var minimumValue: Double? { didSet { reloadErrorTexts() } }
public var characterSet: CharacterSet? { didSet { reloadErrorTexts() } }
public var format: String? { didSet { reloadErrorTexts() } }
public var errors = ValidationErrors()

public init(name: String? = nil, minLength: Int = 0, maxLength: Int? = nil, minValue: Double? = nil, maxValue: Double? = nil, charSet: CharacterSet? = nil, format: String? = nil) {
self.name = name
minimumLength = minLength
maximumLength = maxLength
if maximumLength != nil {
assert(minimumLength <= maximumLength!, "Invalid min/max length bounds.")
}
minimumValue = minValue
maximumValue = maxValue
if let min = minimumValue, let max = maximumValue {
assert(min <= max, "Invalid min/max value bounds.")
}
characterSet = charSet
self.format = format
// configure default errors text
reloadErrorTexts()
}

/// Sets default texts for different error types. Depends on the `name` field, it must be non-nil to proceed.
public func reloadErrorTexts() {
guard let name = name else {
return
}
if errors.minimumLength.text == nil && minimumLength > 0 {
errors.minimumLength.text = (
minimumLength == 1
? "\(name) is required."
: "\(name) must contain \(minimumLength) characters or more."
)
}
if errors.maximumLength.text == nil && maximumLength != nil {
errors.maximumLength.text = "\(name) must contain \(maximumLength!) characters or less."
}
if errors.minimumValue.text == nil && minimumValue != nil {
errors.minimumValue.text = "\(name) must be greater than or equal to \(minimumValue!)."
}
if errors.maximumValue.text == nil && maximumValue != nil {
errors.maximumValue.text = "\(name) must be less than or equal to \(maximumValue!)."
}
if errors.characterSet.text == nil && characterSet != nil {
errors.characterSet.text = "\(name) must contain only characters in set \(characterSet!)."
}
if errors.format.text == nil && self.format != nil {
errors.format.text = "\(name) has invalid format."
}
}

// Making complete false will cause minimumLength, minimumValue and format to be ignored
// this is useful for partial validations, or validations where the final string is
// in process of been completed. For example when entering characters into an UITextField
public func validateString(_ string: String, complete: Bool = true) -> Bool {
var valid = true

errors.reset()

if complete {
valid = (string.count >= minimumLength)
errors.minimumLength.occured = !valid
}

if valid {
if let maximumLength = self.maximumLength {
if let maximumLength = self.maximumLength {
if valid {
valid = (string.count <= maximumLength)
}
errors.maximumLength.occured = !valid
}

if valid {
let formatter = NumberFormatter()
let number = formatter.number(from: string)
if let number = number {
if let maximumValue = self.maximumValue {
let formatter = NumberFormatter()
let number = formatter.number(from: string)
if let number = number {
if let maximumValue = self.maximumValue {
if valid {
valid = (number.doubleValue <= maximumValue)
}
errors.maximumValue.occured = !valid
}

if valid && complete {
if let minimumValue = self.minimumValue {
if complete {
if let minimumValue = self.minimumValue {
if valid {
valid = (number.doubleValue >= minimumValue)
}
errors.minimumValue.occured = !valid
}
}
}

if valid {
if let characterSet = self.characterSet {
let stringCharacterSet = CharacterSet(charactersIn: string)
if let characterSet = self.characterSet {
let stringCharacterSet = CharacterSet(charactersIn: string)
if valid {
valid = characterSet.superSetOf(other: stringCharacterSet)
}
errors.characterSet.occured = !valid
}

if valid && complete {
if complete {
if let format = self.format {
let regex = try! NSRegularExpression(pattern: format, options: .caseInsensitive)
let range = regex.rangeOfFirstMatch(in: string, options: .reportProgress, range: NSRange(location: 0, length: string.count))
valid = (range.location == 0 && range.length == string.count)
if valid {
valid = (range.location == 0 && range.length == string.count)
}
errors.format.occured = !valid
}
}

Expand Down