forked from 3lvis/FormTextField
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Field.swift
101 lines (82 loc) · 3.37 KB
/
Field.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import UIKit
import FormTextField
enum FieldType {
case header, field
}
struct Field {
let type: FieldType
let title: String
let placeholder: String?
var inputType: FormTextFieldInputType = .default
var inputValidator: InputValidatable?
var formatter: Formattable?
init(type: FieldType, title: String, placeholder: String? = nil) {
self.type = type
self.title = title
self.placeholder = placeholder
}
static func fields() -> [Field] {
var items = [Field]()
items.append(Field(type: .header, title: "Cardholder"))
var requiredValidation = Validation()
requiredValidation.minimumLength = 1
let requiredInputValidator = InputValidator(validation: requiredValidation)
let emailField: Field = {
var field = Field(type: .field, title: "Email")
field.inputType = .email
var validation = Validation()
validation.minimumLength = 1
validation.format = "[\\w._%+-]+@[\\w.-]+\\.\\w{2,}"
field.inputValidator = InputValidator(validation: validation)
return field
}()
items.append(emailField)
let UsernameField: Field = {
var field = Field(type: .field, title: "Username")
field.inputType = .name
field.inputValidator = requiredInputValidator
return field
}()
items.append(UsernameField)
items.append(Field(type: .header, title: "Billing info"))
let cardNumberField: Field = {
var field = Field(type: .field, title: "Number", placeholder: "Card Number")
field.inputType = .integer
field.formatter = CardNumberFormatter()
var validation = Validation()
validation.minimumLength = "1234 5678 1234 5678".count
validation.maximumLength = "1234 5678 1234 5678".count
let characterSet = NSMutableCharacterSet.decimalDigit()
characterSet.addCharacters(in: " ")
validation.characterSet = characterSet as CharacterSet
let inputValidator = InputValidator(validation: validation)
field.inputValidator = inputValidator
return field
}()
items.append(cardNumberField)
let expirationDateField: Field = {
var field = Field(type: .field, title: "Expires", placeholder: "MM/YY")
field.formatter = CardExpirationDateFormatter()
field.inputType = .integer
var validation = Validation()
validation.minimumLength = 1
let inputValidator = CardExpirationDateInputValidator(validation: validation)
field.inputValidator = inputValidator
return field
}()
items.append(expirationDateField)
let securityCodeField: Field = {
var field = Field(type: .field, title: "CVC", placeholder: "Security Code")
field.inputType = .integer
var validation = Validation()
validation.maximumLength = "CVC".count
validation.minimumLength = "CVC".count
validation.characterSet = CharacterSet.decimalDigits
let inputValidator = InputValidator(validation: validation)
field.inputValidator = inputValidator
return field
}()
items.append(securityCodeField)
return items
}
}