Skip to content

Commit 624357f

Browse files
committed
sort of crappy demo
1 parent 426a1d7 commit 624357f

8 files changed

Lines changed: 423 additions & 18 deletions

File tree

Validator.xcodeproj/project.xcworkspace/xcshareddata/Validator.xccheckout

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,29 @@
1010
<string>Validator</string>
1111
<key>IDESourceControlProjectOriginsDictionary</key>
1212
<dict>
13-
<key>44BA2A1DF8B8E3EE0CCBE8F37625F38B9979A032</key>
13+
<key>42B8B6166721AE55A9AFCAC5F4DF5276CF0E0973</key>
1414
<string>github.com:jpotts18/swift-validator.git</string>
1515
</dict>
1616
<key>IDESourceControlProjectPath</key>
1717
<string>Validator.xcodeproj</string>
1818
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
1919
<dict>
20-
<key>44BA2A1DF8B8E3EE0CCBE8F37625F38B9979A032</key>
20+
<key>42B8B6166721AE55A9AFCAC5F4DF5276CF0E0973</key>
2121
<string>../..</string>
2222
</dict>
2323
<key>IDESourceControlProjectURL</key>
2424
<string>github.com:jpotts18/swift-validator.git</string>
2525
<key>IDESourceControlProjectVersion</key>
2626
<integer>111</integer>
2727
<key>IDESourceControlProjectWCCIdentifier</key>
28-
<string>44BA2A1DF8B8E3EE0CCBE8F37625F38B9979A032</string>
28+
<string>42B8B6166721AE55A9AFCAC5F4DF5276CF0E0973</string>
2929
<key>IDESourceControlProjectWCConfigurations</key>
3030
<array>
3131
<dict>
3232
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
3333
<string>public.vcs.git</string>
3434
<key>IDESourceControlWCCIdentifierKey</key>
35-
<string>44BA2A1DF8B8E3EE0CCBE8F37625F38B9979A032</string>
35+
<string>42B8B6166721AE55A9AFCAC5F4DF5276CF0E0973</string>
3636
<key>IDESourceControlWCCName</key>
3737
<string>Validator</string>
3838
</dict>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Bucket
3+
type = "1"
4+
version = "2.0">
5+
</Bucket>

Validator/Base.lproj/Main.storyboard

Lines changed: 334 additions & 3 deletions
Large diffs are not rendered by default.

Validator/ValidationError.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
import Foundation
10+
import UIKit
1011

1112
class ValidationError {
1213
let textField:UITextField

Validator/ValidationRule.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
import Foundation
10+
import UIKit
1011

1112
class ValidationRule {
1213
let textField:UITextField

Validator/Validator.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
import Foundation
10+
import UIKit
1011

1112
protocol ValidationDelegate {
1213
func validationWasSuccessful()
@@ -19,15 +20,15 @@ protocol ValidationFieldDelegate {
1920
}
2021

2122
class Validator {
23+
// dictionary to handle complex view hierarchies like dynamic tableview cells
2224
var validationRules:[String:ValidationRule] = [:]
2325
var validationErrors:[String:ValidationError] = [:]
24-
let delegate:ValidationDelegate
26+
27+
init(){}
2528

26-
init(delegate:ValidationDelegate){
27-
self.delegate = delegate
28-
}
29+
// MARK: Using Keys
2930

30-
func registerField(key:String, textField:UITextField, rules:[ValidationRuleType]) {
31+
func registerFieldByKey(key:String, textField:UITextField, rules:[ValidationRuleType]) {
3132
validationRules[key] = ValidationRule(textField: textField, rules: rules)
3233
}
3334

@@ -41,9 +42,9 @@ class Validator {
4142
}
4243
}
4344

44-
func validateAllBy(keys:[String], delegate:ValidationDelegate){
45+
func validateAllKeys(delegate:ValidationDelegate){
4546

46-
for key in keys {
47+
for key in validationRules.keys {
4748
if let currentRule:ValidationRule = validationRules[key] {
4849
if var error:ValidationError = currentRule.validateField() {
4950
validationErrors[key] = error

Validator/ViewController.swift

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,88 @@
88

99
import UIKit
1010

11-
class ViewController: UIViewController {
11+
class ViewController: UIViewController , ValidationDelegate {
1212

13+
// TextFields
14+
@IBOutlet weak var fullNameTextField: UITextField!
15+
@IBOutlet weak var emailTextField: UITextField!
16+
@IBOutlet weak var phoneNumberTextField: UITextField!
17+
@IBOutlet weak var zipcodeTextField: UITextField!
18+
19+
// Error Labels
20+
@IBOutlet weak var fullNameErrorLabel: UILabel!
21+
@IBOutlet weak var emailErrorLabel: UILabel!
22+
@IBOutlet weak var phoneNumberErrorLabel: UILabel!
23+
@IBOutlet weak var zipcodeErrorLabel: UILabel!
24+
25+
let KEYS = ["Full Name", "Email", "Phone", "ZipCode"]
26+
27+
let validator = Validator()
28+
1329
override func viewDidLoad() {
1430
super.viewDidLoad()
15-
// Do any additional setup after loading the view, typically from a nib.
31+
32+
validator.registerFieldByKey(KEYS[0], textField: fullNameTextField, rules: [.Required, .FullName])
33+
validator.registerFieldByKey(KEYS[1], textField: emailTextField, rules: [.Required, .Email])
34+
validator.registerFieldByKey(KEYS[2], textField: phoneNumberTextField, rules: [.Required, .PhoneNumber])
35+
validator.registerFieldByKey(KEYS[3], textField: zipcodeTextField, rules: [.Required, .ZipCode])
1636

17-
var validator = Validator(delegate: self)
1837
}
1938

2039
override func didReceiveMemoryWarning() {
2140
super.didReceiveMemoryWarning()
2241
// Dispose of any resources that can be recreated.
2342

24-
2543
}
2644

45+
@IBAction func submitTapped(sender: AnyObject) {
46+
println("submit tapped")
47+
validator.validateAllKeys(self)
48+
}
49+
50+
// MARK: ValidationDelegate Methods
51+
52+
func validationFailed(errors: [String : ValidationError]) {
53+
54+
if var fullNameError = errors[KEYS[0]] {
55+
setError(fullNameErrorLabel, error: fullNameError)
56+
} else {
57+
removeError(fullNameErrorLabel, textField: fullNameTextField)
58+
}
59+
60+
if var emailNameError = errors[KEYS[1]] {
61+
setError(emailErrorLabel, error: emailNameError)
62+
} else {
63+
removeError(emailErrorLabel, textField: emailTextField)
64+
}
65+
66+
if var phoneError = errors[KEYS[2]] {
67+
setError(phoneNumberErrorLabel, error: phoneError)
68+
} else {
69+
removeError(phoneNumberErrorLabel, textField: phoneNumberTextField)
70+
}
71+
72+
if var zipError = errors[KEYS[3]] {
73+
setError(zipcodeErrorLabel, error: zipError)
74+
} else {
75+
removeError(zipcodeErrorLabel, textField: zipcodeTextField)
76+
}
77+
}
78+
79+
func setError(label:UILabel, error:ValidationError) {
80+
label.hidden = false
81+
label.text = error.error.description()
82+
error.textField.layer.borderColor = UIColor.redColor().CGColor
83+
error.textField.layer.borderWidth = 2.0
84+
}
85+
func removeError(label:UILabel, textField:UITextField) {
86+
label.hidden = true
87+
textField.layer.borderWidth = 0.0
88+
}
89+
90+
func validationWasSuccessful() {
91+
92+
}
2793

2894
}
2995

0 commit comments

Comments
 (0)