Skip to content

Commit

Permalink
Added operator for configurable auto trimming
Browse files Browse the repository at this point in the history
  • Loading branch information
sanzaru committed Oct 17, 2024
1 parent 791021c commit 23b303a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Button { } label: {
| .pattern(String) | If set, the value must match the provided regular expression. | ```.pattern("[a-zA-Z]")``` |
| .email | If set, the value must match an internal regular expression for email addresses. | ```.email``` |
| .phone | If set, the value must match an internal regular expression for phone numbers. | ```.phone``` |
| .disableTrimming | If set, the automatic trimming of white spaces and new lines is disabled. | ```.disableTrimming``` |


## Example
Expand Down
9 changes: 8 additions & 1 deletion Sources/Formify/FormifyField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ public struct FormifyField {
isTouched = true
}

if !disableTrimming {
value = value.trimmingCharacters(in: .whitespacesAndNewlines)
}

validate()
value = value.trimmingCharacters(in: .whitespacesAndNewlines)
}
}
}
Expand All @@ -29,6 +32,8 @@ public struct FormifyField {
public private(set) var maxLength: Int?
public private(set) var pattern: Regex<Substring>?

private var disableTrimming = false

public var isValid: Bool {
isRequired && value.isEmpty ? false : errors.isEmpty
}
Expand All @@ -50,6 +55,8 @@ public struct FormifyField {
pattern = try? Regex(FormifyDefaultPattern.email.rawValue)
case .phonenumber:
pattern = try? Regex(FormifyDefaultPattern.phone.rawValue)
case .disableTrimming:
disableTrimming = true
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Sources/Formify/FormifyOperator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public enum FormifyOperator {
case maxLength(Int)
case email
case phonenumber
case disableTrimming
}

0 comments on commit 23b303a

Please sign in to comment.