Compare commits
42 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
db272a717a | |
|
|
8e0168b270 | |
|
|
f94de389e4 | |
|
|
0190013526 | |
|
|
c0ee801a2d | |
|
|
ba27b0e501 | |
|
|
fb50b97888 | |
|
|
bef1c1802a | |
|
|
4d64561b94 | |
|
|
b3f8d74212 | |
|
|
0debc5d7eb | |
|
|
6cff6cac3e | |
|
|
bfa3252c45 | |
|
|
d89d18b019 | |
|
|
696c600dc0 | |
|
|
2a6c23ad9e | |
|
|
43fddfb385 | |
|
|
1efab45d38 | |
|
|
2c68d3db95 | |
|
|
4f6e77c0aa | |
|
|
55f282fe92 | |
|
|
8bf185d820 | |
|
|
68fdfbdac0 | |
|
|
90e141e923 | |
|
|
8339c8c086 | |
|
|
3dc5b79272 | |
|
|
f8b2d242cf | |
|
|
fbaf18ac67 | |
|
|
62586fe255 | |
|
|
8f224aba83 | |
|
|
5abb05a423 | |
|
|
db6be7b53c | |
|
|
6cfc49799b | |
|
|
2f2e183953 | |
|
|
59d1a6398e | |
|
|
f344bdc370 | |
|
|
ca7ec31141 | |
|
|
9749d9d670 | |
|
|
a094156445 | |
|
|
39530fb7d6 | |
|
|
2fabe418ea | |
|
|
8f116df330 |
21
.travis.yml
21
.travis.yml
|
|
@ -1,15 +1,24 @@
|
|||
language: objective-c
|
||||
osx_image: xcode7.1
|
||||
osx_image: xcode8
|
||||
xcode_sdk: iphonesimulator10.0
|
||||
xcode_project: Validator.xcodeproj
|
||||
xcode_scheme: Validator
|
||||
|
||||
before_install:
|
||||
|
||||
- gem install cocoapods -v '0.32.1'
|
||||
- gem install xcpretty --no-rdoc --no-ri --no-document --quiet
|
||||
|
||||
script:
|
||||
|
||||
- xcodebuild clean build test -project Validator.xcodeproj -scheme Validator -sdk iphonesimulator -destination "platform=iOS Simulator,OS=10.0,name=iPhone 6" -enableCodeCoverage YES CODE_SIGNING_REQUIRED=NO | xcpretty
|
||||
- pod lib lint
|
||||
- xcodebuild clean build test -project Validator.xcodeproj -scheme Validator -sdk iphonesimulator9.1 -destination "OS=9.1,name=iPhone 6" -enableCodeCoverage YES | xcpretty
|
||||
|
||||
after_success:
|
||||
|
||||
- bash <(curl -s https://codecov.io/bash)
|
||||
- bash <(curl -s https://codecov.io/bash)
|
||||
|
||||
|
||||
notifications:
|
||||
webhooks:
|
||||
urls:
|
||||
- https://webhooks.gitter.im/e/4cfa929bd227586305cc
|
||||
on_success: change # options: [always|never|change] default: always
|
||||
on_failure: always # options: [always|never|change] default: always
|
||||
|
|
|
|||
43
README.md
43
README.md
|
|
@ -1,7 +1,7 @@
|
|||
SwiftValidator
|
||||
===============
|
||||
|
||||
[](https://travis-ci.org/jpotts18/SwiftValidator) [](https://github.com/Carthage/Carthage) [](https://codecov.io/github/jpotts18/SwiftValidator?branch=master)
|
||||
[](https://travis-ci.org/SwiftValidatorCommunity/SwiftValidator) [](https://codecov.io/github/SwiftValidatorCommunity/SwiftValidator?branch=master)
|
||||
|
||||
Swift Validator is a rule-based validation library for Swift.
|
||||
|
||||
|
|
@ -21,10 +21,18 @@ source 'https://github.com/CocoaPods/Specs.git'
|
|||
platform :ios, "8.1"
|
||||
|
||||
use_frameworks!
|
||||
# For SwiftValidator tag 3.0.3
|
||||
pod 'SwiftValidator', '3.0.3'
|
||||
# Recommended: For the most recent version of SwiftValidator (master branch)
|
||||
|
||||
# Swift 3
|
||||
# Extended beyond UITextField
|
||||
pod 'SwiftValidator', :git => 'https://github.com/jpotts18/SwiftValidator.git', :branch => 'master'
|
||||
|
||||
# Swift 2.1
|
||||
# Extended beyond UITextField
|
||||
# Note: Installing 4.x.x will break code from 3.x.x
|
||||
pod 'SwiftValidator', :git => 'https://github.com/jpotts18/SwiftValidator.git', :tag => '4.0.0'
|
||||
|
||||
# Swift 2.1 (limited to UITextField validation)
|
||||
pod 'SwiftValidator', :git => 'https://github.com/jpotts18/SwiftValidator.git', :tag => '3.0.5'
|
||||
```
|
||||
|
||||
Install into your project:
|
||||
|
|
@ -74,7 +82,7 @@ override func viewDidLoad() {
|
|||
|
||||
// You can now pass in regex and length parameters through overloaded contructors
|
||||
validator.registerField(phoneNumberTextField, errorLabel: phoneNumberErrorLabel, rules: [RequiredRule(), MinLengthRule(length: 9)])
|
||||
validator.registerField(zipcodeTextField, errorLabel: zipcodeErrorLabel, rules: [RequiredRule(), ZipCodeRule(regex = "\\d{5}")])
|
||||
validator.registerField(zipcodeTextField, errorLabel: zipcodeErrorLabel, rules: [RequiredRule(), ZipCodeRule(regex : "\\d{5}")])
|
||||
|
||||
// You can unregister a text field if you no longer want to validate it
|
||||
validator.unregisterField(fullNameTextField)
|
||||
|
|
@ -86,7 +94,7 @@ Validate Fields on button tap or however you would like to trigger it.
|
|||
|
||||
```swift
|
||||
@IBAction func signupTapped(sender: AnyObject) {
|
||||
validator.validate(delegate:self)
|
||||
validator.validate(self)
|
||||
}
|
||||
```
|
||||
|
||||
|
|
@ -99,14 +107,16 @@ func validationSuccessful() {
|
|||
// submit the form
|
||||
}
|
||||
|
||||
func validationFailed(errors:[UITextField:ValidationError]) {
|
||||
// turn the fields to red
|
||||
for (field, error) in validator.errors {
|
||||
field.layer.borderColor = UIColor.redColor().CGColor
|
||||
field.layer.borderWidth = 1.0
|
||||
error.errorLabel?.text = error.errorMessage // works if you added labels
|
||||
error.errorLabel?.hidden = false
|
||||
}
|
||||
func validationFailed(_ errors:[(Validatable ,ValidationError)]) {
|
||||
// turn the fields to red
|
||||
for (field, error) in errors {
|
||||
if let field = field as? UITextField {
|
||||
field.layer.borderColor = UIColor.red.cgColor
|
||||
field.layer.borderWidth = 1.0
|
||||
}
|
||||
error.errorLabel?.text = error.errorMessage // works if you added labels
|
||||
error.errorLabel?.isHidden = false
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
|
@ -149,13 +159,14 @@ class SSNVRule: RegexRule {
|
|||
```
|
||||
|
||||
## Documentation
|
||||
Checkout the docs <a href="http://jpotts18.github.io/SwiftValidator/">here</a> via [@jazzydocs](https://twitter.com/jazzydocs).
|
||||
Checkout the docs <a href="http://swiftvalidatorcommunity.github.io/SwiftValidator/">here</a> via [@jazzydocs](https://twitter.com/jazzydocs).
|
||||
|
||||
|
||||
Credits
|
||||
-------
|
||||
|
||||
Swift Validator is written and maintained by Jeff Potter [@jpotts18](http://twitter.com/jpotts18). David Patterson [@dave_tw12](http://twitter.com/dave_tw12) actively works as a collaborator.
|
||||
Swift Validator is written and maintained by Jeff Potter [@jpotts18](http://twitter.com/jpotts18). David Patterson [@dave_tw12](http://twitter.com/dave_tw12) actively works as a collaborator. Special thanks to [Deniz Adalar](https://github.com/dadalar) for
|
||||
adding validation beyond UITextField.
|
||||
|
||||
## Contributing
|
||||
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
Pod::Spec.new do |s|
|
||||
s.name = "SwiftValidator"
|
||||
s.version = "3.0.1"
|
||||
s.version = "4.0.2"
|
||||
s.summary = "A UITextField Validation library for Swift"
|
||||
s.homepage = "https://github.com/jpotts18/SwiftValidator"
|
||||
s.homepage = "https://github.com/TouchInstinct/SwiftValidator"
|
||||
s.screenshots = "https://raw.githubusercontent.com/jpotts18/SwiftValidator/master/swift-validator-v2.gif"
|
||||
s.license = { :type => "MIT", :file => "LICENSE.txt" }
|
||||
s.author = { "Jeff Potter" => "jeff.potter6@gmail.com" }
|
||||
s.social_media_url = "http://twitter.com/jpotts18"
|
||||
s.platform = :ios
|
||||
s.ios.deployment_target = '8.0'
|
||||
s.source = { :git => "https://github.com/jpotts18/SwiftValidator.git", :tag => "3.0.1" }
|
||||
s.source = { :git => "https://github.com/TouchInstinct/SwiftValidator.git", :tag => s.version }
|
||||
s.source_files = "SwiftValidator/**/*.swift"
|
||||
s.exclude_files = "Validator/AppDelegate.swift"
|
||||
s.frameworks = ['Foundation', 'UIKit']
|
||||
|
|
|
|||
|
|
@ -0,0 +1,25 @@
|
|||
//
|
||||
// Validatable.swift
|
||||
// Validator
|
||||
//
|
||||
// Created by Deniz Adalar on 28/04/16.
|
||||
// Copyright © 2016 jpotts18. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public typealias ValidatableField = AnyObject & Validatable
|
||||
|
||||
public protocol Validatable {
|
||||
|
||||
var validationText: String {
|
||||
get
|
||||
}
|
||||
}
|
||||
|
||||
extension UITextField: Validatable {
|
||||
|
||||
public var validationText: String {
|
||||
return text ?? ""
|
||||
}
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@ import UIKit
|
|||
/**
|
||||
Protocol for `ValidationDelegate` adherents, which comes with two required methods that are called depending on whether validation succeeded or failed.
|
||||
*/
|
||||
@objc public protocol ValidationDelegate {
|
||||
public protocol ValidationDelegate {
|
||||
/**
|
||||
This method will be called on delegate object when validation is successful.
|
||||
|
||||
|
|
@ -23,5 +23,5 @@ import UIKit
|
|||
|
||||
- returns: No return value.
|
||||
*/
|
||||
func validationFailed(errors: [UITextField:ValidationError])
|
||||
func validationFailed(_ errors: [(Validatable, ValidationError)])
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,38 +7,26 @@ import Foundation
|
|||
import UIKit
|
||||
|
||||
/**
|
||||
The `ValidationError` class is used for representing errors of a failed validation. It contains the text field, error label, and error message of a failed validation.
|
||||
The `ValidationError` class is used for representing errors of a failed validation. It contains the field, error label, and error message of a failed validation.
|
||||
*/
|
||||
public class ValidationError: NSObject {
|
||||
/// the textField of the field
|
||||
public let textField:UITextField
|
||||
/// the Validatable field of the field
|
||||
public let field:ValidatableField
|
||||
/// the error label of the field
|
||||
public var errorLabel:UILabel?
|
||||
/// the error message of the field
|
||||
public let errorMessage:String
|
||||
|
||||
/**
|
||||
Initializes `ValidationError` object with a textField and error.
|
||||
|
||||
- parameter textField: UITextField that holds textField.
|
||||
- parameter errorMessage: String that holds error message.
|
||||
- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
|
||||
*/
|
||||
public init(textField:UITextField, error:String){
|
||||
self.textField = textField
|
||||
self.errorMessage = error
|
||||
}
|
||||
|
||||
/**
|
||||
Initializes `ValidationError` object with a textField, errorLabel, and errorMessage.
|
||||
|
||||
- parameter textField: UITextField that holds textField.
|
||||
Initializes `ValidationError` object with a field, errorLabel, and errorMessage.
|
||||
|
||||
- parameter field: Validatable field that holds field.
|
||||
- parameter errorLabel: UILabel that holds error label.
|
||||
- parameter errorMessage: String that holds error message.
|
||||
- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
|
||||
*/
|
||||
public init(textField:UITextField, errorLabel:UILabel?, error:String){
|
||||
self.textField = textField
|
||||
public init(field:ValidatableField, errorLabel:UILabel?, error:String){
|
||||
self.field = field
|
||||
self.errorLabel = errorLabel
|
||||
self.errorMessage = error
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,13 +14,15 @@ import UIKit
|
|||
*/
|
||||
public class Validator {
|
||||
/// Dictionary to hold all fields (and accompanying rules) that will undergo validation.
|
||||
public var validations = [UITextField:ValidationRule]()
|
||||
public var validations = ValidatorDictionary<ValidationRule>()
|
||||
/// Dictionary to hold fields (and accompanying errors) that were unsuccessfully validated.
|
||||
public var errors = [UITextField:ValidationError]()
|
||||
/// Variable that holds success closure to display positive status of field.
|
||||
private var successStyleTransform:((validationRule:ValidationRule)->Void)?
|
||||
public var errors = ValidatorDictionary<ValidationError>()
|
||||
/// Dictionary to hold fields by their object identifiers
|
||||
private var fields = ValidatorDictionary<Validatable>()
|
||||
/// Variable that holds success closure to display positive status of field.
|
||||
private var successStyleTransform:((_ validationRule:ValidationRule)->Void)?
|
||||
/// Variable that holds error closure to display negative status of field.
|
||||
private var errorStyleTransform:((validationError:ValidationError)->Void)?
|
||||
private var errorStyleTransform:((_ validationError:ValidationError)->Void)?
|
||||
/// - returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
|
||||
public init(){}
|
||||
|
||||
|
|
@ -34,21 +36,21 @@ public class Validator {
|
|||
*/
|
||||
private func validateAllFields() {
|
||||
|
||||
errors = [:]
|
||||
errors = ValidatorDictionary<ValidationError>()
|
||||
|
||||
for (textField, rule) in validations {
|
||||
for (_, rule) in validations {
|
||||
if let error = rule.validateField() {
|
||||
errors[textField] = error
|
||||
errors[rule.field] = error
|
||||
|
||||
// let the user transform the field if they want
|
||||
if let transform = self.errorStyleTransform {
|
||||
transform(validationError: error)
|
||||
transform(error)
|
||||
}
|
||||
} else {
|
||||
// No error
|
||||
// let the user transform the field if they want
|
||||
if let transform = self.successStyleTransform {
|
||||
transform(validationRule: rule)
|
||||
transform(rule)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -60,25 +62,25 @@ public class Validator {
|
|||
This method is used to validate a single field registered to Validator. If validation is unsuccessful,
|
||||
field gets added to errors dictionary.
|
||||
|
||||
- parameter textField: Holds validator field data.
|
||||
- parameter field: Holds validator field data.
|
||||
- returns: No return value.
|
||||
*/
|
||||
public func validateField(textField: UITextField, callback: (error:ValidationError?) -> Void){
|
||||
if let fieldRule = validations[textField] {
|
||||
public func validateField(_ field: ValidatableField, callback: (_ error:ValidationError?) -> Void){
|
||||
if let fieldRule = validations[field] {
|
||||
if let error = fieldRule.validateField() {
|
||||
errors[textField] = error
|
||||
errors[field] = error
|
||||
if let transform = self.errorStyleTransform {
|
||||
transform(validationError: error)
|
||||
transform(error)
|
||||
}
|
||||
callback(error: error)
|
||||
callback(error)
|
||||
} else {
|
||||
if let transform = self.successStyleTransform {
|
||||
transform(validationRule: fieldRule)
|
||||
transform(fieldRule)
|
||||
}
|
||||
callback(error: nil)
|
||||
callback(nil)
|
||||
}
|
||||
} else {
|
||||
callback(error: nil)
|
||||
callback(nil)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -91,43 +93,33 @@ public class Validator {
|
|||
- parameter error: A closure which is called with validationError, an object that holds validation error data
|
||||
- returns: No return value
|
||||
*/
|
||||
public func styleTransformers(success success:((validationRule:ValidationRule)->Void)?, error:((validationError:ValidationError)->Void)?) {
|
||||
public func styleTransformers(success:((_ validationRule:ValidationRule)->Void)?, error:((_ validationError:ValidationError)->Void)?) {
|
||||
self.successStyleTransform = success
|
||||
self.errorStyleTransform = error
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
This method is used to add a field to validator.
|
||||
|
||||
- parameter textField: field that is to be validated.
|
||||
- parameter Rule: An array which holds different rules to validate against textField.
|
||||
- returns: No return value
|
||||
*/
|
||||
public func registerField(textField:UITextField, rules:[Rule]) {
|
||||
validations[textField] = ValidationRule(textField: textField, rules: rules, errorLabel: nil)
|
||||
}
|
||||
|
||||
/**
|
||||
This method is used to add a field to validator.
|
||||
|
||||
- parameter textfield: field that is to be validated.
|
||||
- parameter field: field that is to be validated.
|
||||
- parameter errorLabel: A UILabel that holds error label data
|
||||
- parameter rules: A Rule array that holds different rules that apply to said textField.
|
||||
- parameter rules: A Rule array that holds different rules that apply to said field.
|
||||
- returns: No return value
|
||||
*/
|
||||
public func registerField(textField:UITextField, errorLabel:UILabel, rules:[Rule]) {
|
||||
validations[textField] = ValidationRule(textField: textField, rules:rules, errorLabel:errorLabel)
|
||||
public func registerField(_ field: ValidatableField, errorLabel:UILabel? = nil, rules:[Rule]) {
|
||||
validations[field] = ValidationRule(field: field, rules:rules, errorLabel:errorLabel)
|
||||
fields[field] = field
|
||||
}
|
||||
|
||||
/**
|
||||
This method is for removing a field validator.
|
||||
|
||||
- parameter textField: field used to locate and remove textField from validator.
|
||||
- parameter field: field used to locate and remove field from validator.
|
||||
- returns: No return value
|
||||
*/
|
||||
public func unregisterField(textField:UITextField) {
|
||||
validations.removeValueForKey(textField)
|
||||
errors.removeValueForKey(textField)
|
||||
public func unregisterField(_ field:ValidatableField) {
|
||||
validations.removeValueForKey(field)
|
||||
errors.removeValueForKey(field)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -135,14 +127,14 @@ public class Validator {
|
|||
|
||||
- returns: No return value.
|
||||
*/
|
||||
public func validate(delegate:ValidationDelegate) {
|
||||
public func validate(_ delegate:ValidationDelegate) {
|
||||
|
||||
self.validateAllFields()
|
||||
|
||||
if errors.isEmpty {
|
||||
delegate.validationSuccessful()
|
||||
} else {
|
||||
delegate.validationFailed(errors)
|
||||
delegate.validationFailed(errors.map { (fields[$1.field]!, $1) })
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -150,13 +142,13 @@ public class Validator {
|
|||
/**
|
||||
This method validates all fields in validator and sets any errors to errors parameter of callback.
|
||||
|
||||
- parameter callback: A closure which is called with errors, a dictionary of type UITextField:ValidationError.
|
||||
- parameter callback: A closure which is called with errors, a dictionary of type Validatable:ValidationError.
|
||||
- returns: No return value.
|
||||
*/
|
||||
public func validate(callback:(errors:[UITextField:ValidationError])->Void) -> Void {
|
||||
public func validate(_ callback:(_ errors:[(Validatable, ValidationError)])->Void) -> Void {
|
||||
|
||||
self.validateAllFields()
|
||||
|
||||
callback(errors: errors)
|
||||
callback(errors.map { (fields[$1.field]!, $1) } )
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
//
|
||||
// ValidatorDictionary.swift
|
||||
// Validator
|
||||
//
|
||||
// Created by Deniz Adalar on 04/05/16.
|
||||
// Copyright © 2016 jpotts18. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public struct ValidatorDictionary<T> : Sequence {
|
||||
|
||||
private var innerDictionary: [ObjectIdentifier: T] = [:];
|
||||
|
||||
public subscript(key: ValidatableField?) -> T? {
|
||||
get {
|
||||
if let key = key {
|
||||
return innerDictionary[ObjectIdentifier(key)];
|
||||
} else {
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
set(newValue) {
|
||||
if let key = key {
|
||||
innerDictionary[ObjectIdentifier(key)] = newValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public mutating func removeAll() {
|
||||
innerDictionary.removeAll()
|
||||
}
|
||||
|
||||
public mutating func removeValueForKey(_ key: ValidatableField) {
|
||||
innerDictionary.removeValue(forKey: ObjectIdentifier(key))
|
||||
}
|
||||
|
||||
public var isEmpty: Bool {
|
||||
return innerDictionary.isEmpty
|
||||
}
|
||||
|
||||
public func makeIterator() -> DictionaryIterator<ObjectIdentifier ,T> {
|
||||
return innerDictionary.makeIterator()
|
||||
}
|
||||
}
|
||||
|
|
@ -21,6 +21,6 @@ public class AlphaNumericRule: CharacterSetRule {
|
|||
- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
|
||||
*/
|
||||
public init(message: String = "Enter valid numeric characters") {
|
||||
super.init(characterSet: NSCharacterSet.alphanumericCharacterSet(), message: message)
|
||||
super.init(characterSet: CharacterSet.alphanumerics, message: message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,6 @@ public class AlphaRule: CharacterSetRule {
|
|||
- returns: An initialized object, or nil if an object could not be created for some reason.
|
||||
*/
|
||||
public init(message: String = "Enter valid alphabetic characters") {
|
||||
super.init(characterSet: NSCharacterSet.letterCharacterSet(), message: message)
|
||||
super.init(characterSet: CharacterSet.letters, message: message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ import Foundation
|
|||
*/
|
||||
public class CharacterSetRule: Rule {
|
||||
/// NSCharacter that hold set of valid characters to hold
|
||||
private let characterSet: NSCharacterSet
|
||||
private let characterSet: CharacterSet
|
||||
/// String that holds error message
|
||||
private var message: String
|
||||
|
||||
|
|
@ -24,7 +24,7 @@ public class CharacterSetRule: Rule {
|
|||
- parameter message: String of error message.
|
||||
- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
|
||||
*/
|
||||
public init(characterSet: NSCharacterSet, message: String = "Enter valid alpha") {
|
||||
public init(characterSet: CharacterSet, message: String = "Enter valid alpha") {
|
||||
self.characterSet = characterSet
|
||||
self.message = message
|
||||
}
|
||||
|
|
@ -35,9 +35,9 @@ public class CharacterSetRule: Rule {
|
|||
- parameter value: String to checked for validation.
|
||||
- returns: Boolean value. True if validation is successful; False if validation fails.
|
||||
*/
|
||||
public func validate(value: String) -> Bool {
|
||||
public func validate(_ value: String) -> Bool {
|
||||
for uni in value.unicodeScalars {
|
||||
if !characterSet.longCharacterIsMember(uni.value) {
|
||||
guard let uniVal = UnicodeScalar(uni.value), characterSet.contains(uniVal) else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
@ -45,11 +45,11 @@ public class CharacterSetRule: Rule {
|
|||
}
|
||||
|
||||
/**
|
||||
Displays error message when text field fails validation.
|
||||
Displays error message when field fails validation.
|
||||
|
||||
- returns: String of error message.
|
||||
*/
|
||||
public func errorMessage() -> String {
|
||||
return message
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,35 +10,35 @@ import Foundation
|
|||
import UIKit
|
||||
|
||||
/**
|
||||
`ConfirmationRule` is a subclass of Rule that defines how a text field that has to be equal
|
||||
to another text field is validated.
|
||||
`ConfirmationRule` is a subclass of Rule that defines how a field that has to be equal
|
||||
to another field is validated.
|
||||
*/
|
||||
public class ConfirmationRule: Rule {
|
||||
/// parameter confirmField: text field to which original text field will be compared to.
|
||||
private let confirmField: UITextField
|
||||
/// parameter confirmField: field to which original text field will be compared to.
|
||||
private let confirmField: ValidatableField
|
||||
/// parameter message: String of error message.
|
||||
private var message : String
|
||||
|
||||
/**
|
||||
Initializes a `ConfirmationRule` object to validate the text of a text field that should equal the text of another text field.
|
||||
Initializes a `ConfirmationRule` object to validate the text of a field that should equal the text of another field.
|
||||
|
||||
- parameter confirmField: text field to which original text field will be compared to.
|
||||
- parameter confirmField: field to which original field will be compared to.
|
||||
- parameter message: String of error message.
|
||||
- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
|
||||
*/
|
||||
public init(confirmField: UITextField, message : String = "This field does not match"){
|
||||
public init(confirmField: ValidatableField, message : String = "This field does not match"){
|
||||
self.confirmField = confirmField
|
||||
self.message = message
|
||||
}
|
||||
|
||||
/**
|
||||
Used to validate a text field.
|
||||
Used to validate a field.
|
||||
|
||||
- parameter value: String to checked for validation.
|
||||
- returns: A boolean value. True if validation is successful; False if validation fails.
|
||||
*/
|
||||
public func validate(value: String) -> Bool {
|
||||
return confirmField.text == value
|
||||
public func validate(_ value: String) -> Bool {
|
||||
return confirmField.validationText == value
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -49,4 +49,4 @@ public class ConfirmationRule: Rule {
|
|||
public func errorMessage() -> String {
|
||||
return message
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,10 +13,10 @@ import Foundation
|
|||
public class EmailRule: RegexRule {
|
||||
|
||||
/// Regular express string to be used in validation.
|
||||
static let regex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
|
||||
static let regex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
|
||||
|
||||
/**
|
||||
Initializes an `EmailRule` object to validate an email text field.
|
||||
Initializes an `EmailRule` object to validate an email field.
|
||||
|
||||
- parameter message: String of error message.
|
||||
- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
import Foundation
|
||||
|
||||
/**
|
||||
`ExactLengthRule` is a subclass of Rule that is used to make sure a the text of a text field is an exact length.
|
||||
`ExactLengthRule` is a subclass of Rule that is used to make sure a the text of a field is an exact length.
|
||||
*/
|
||||
public class ExactLengthRule : Rule {
|
||||
/// parameter message: String of error message.
|
||||
|
|
@ -18,7 +18,7 @@ public class ExactLengthRule : Rule {
|
|||
private var length : Int
|
||||
|
||||
/**
|
||||
Initializes an `ExactLengthRule` object to validate the text of a text field against an exact length.
|
||||
Initializes an `ExactLengthRule` object to validate the text of a field against an exact length.
|
||||
|
||||
- parameter length: Integer value of exact string length being specified.
|
||||
- parameter message: String of error message.
|
||||
|
|
@ -26,25 +26,25 @@ public class ExactLengthRule : Rule {
|
|||
*/
|
||||
public init(length: Int, message : String = "Must be exactly %ld characters long"){
|
||||
self.length = length
|
||||
self.message = NSString(format: message, self.length) as String
|
||||
self.message = String(format: message, self.length)
|
||||
}
|
||||
|
||||
/**
|
||||
Used to validate a text field.
|
||||
Used to validate a field.
|
||||
|
||||
- parameter value: String to checked for validation.
|
||||
- returns: A boolean value. True if validation is successful; False if validation fails.
|
||||
*/
|
||||
public func validate(value: String) -> Bool {
|
||||
return value.characters.count == length
|
||||
public func validate(_ value: String) -> Bool {
|
||||
return value.count == length
|
||||
}
|
||||
|
||||
/**
|
||||
Displays error message if a text field fails validation.
|
||||
Displays error message if a field fails validation.
|
||||
|
||||
- returns: String of error message.
|
||||
*/
|
||||
public func errorMessage() -> String {
|
||||
return message
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ public class FloatRule:Rule {
|
|||
private var message : String
|
||||
|
||||
/**
|
||||
Initializes a `FloatRule` object to validate that the text of a text field is a floating point number.
|
||||
Initializes a `FloatRule` object to validate that the text of a field is a floating point number.
|
||||
|
||||
- parameter message: String of error message.
|
||||
- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
|
||||
|
|
@ -26,22 +26,22 @@ public class FloatRule:Rule {
|
|||
}
|
||||
|
||||
/**
|
||||
Used to validate text field.
|
||||
Used to validate field.
|
||||
|
||||
- parameter value: String to checked for validation.
|
||||
- returns: Boolean value. True if validation is successful; False if validation fails.
|
||||
*/
|
||||
public func validate(value: String) -> Bool {
|
||||
public func validate(_ value: String) -> Bool {
|
||||
let regex = try? NSRegularExpression(pattern: "^[-+]?(\\d*[.])?\\d+$", options: [])
|
||||
if let regex = regex {
|
||||
let match = regex.numberOfMatchesInString(value, options: [], range: NSRange(location: 0, length: value.characters.count))
|
||||
let match = regex.numberOfMatches(in: value, options: [], range: NSRange(location: 0, length: value.count))
|
||||
return match == 1
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
Displays error message when text field fails validation.
|
||||
Displays error message when field fails validation.
|
||||
|
||||
- returns: String of error message.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ public class FullNameRule : Rule {
|
|||
private var message : String
|
||||
|
||||
/**
|
||||
Initializes a `FullNameRule` object that is used to verify that text in text field is a full name.
|
||||
Initializes a `FullNameRule` object that is used to verify that text in field is a full name.
|
||||
|
||||
- parameter message: String of error message.
|
||||
- returns: An initialized `FullNameRule` object, or nil if an object could not be created for some reason that would not result in an exception.
|
||||
|
|
@ -25,22 +25,22 @@ public class FullNameRule : Rule {
|
|||
}
|
||||
|
||||
/**
|
||||
Used to validate a text field.
|
||||
Used to validate a field.
|
||||
|
||||
- parameter value: String to checked for validation.
|
||||
- returns: A boolean value. True if validation is successful; False if validation fails.
|
||||
*/
|
||||
public func validate(value: String) -> Bool {
|
||||
let nameArray: [String] = value.characters.split { $0 == " " }.map { String($0) }
|
||||
public func validate(_ value: String) -> Bool {
|
||||
let nameArray: [String] = value.split { $0 == " " }.map { String($0) }
|
||||
return nameArray.count >= 2
|
||||
}
|
||||
|
||||
/**
|
||||
Used to display error message of a text field that has failed validation.
|
||||
Used to display error message of a field that has failed validation.
|
||||
|
||||
- returns: String of error message.
|
||||
*/
|
||||
public func errorMessage() -> String {
|
||||
return message
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,24 +27,24 @@ public class ISBNRule: Rule {
|
|||
}
|
||||
|
||||
/**
|
||||
Method used to validate text field.
|
||||
Method used to validate field.
|
||||
|
||||
- parameter value: String to checked for validation.
|
||||
- returns: Boolean value. True if validation is successful; False if validation fails.
|
||||
*/
|
||||
public func validate(value: String) -> Bool {
|
||||
public func validate(_ value: String) -> Bool {
|
||||
|
||||
guard let regex = try? NSRegularExpression(pattern: "[\\s-]", options: []) else {
|
||||
fatalError("Invalid ISBN sanitizing regex")
|
||||
}
|
||||
|
||||
let sanitized = regex.stringByReplacingMatchesInString(value, options: [], range: NSMakeRange(0, value.characters.count), withTemplate: "")
|
||||
let sanitized = regex.stringByReplacingMatches(in: value, options: [], range: NSMakeRange(0, value.count), withTemplate: "")
|
||||
|
||||
return ISBN10Validator().verify(sanitized) || ISBN13Validator().verify(sanitized)
|
||||
}
|
||||
|
||||
/**
|
||||
Method used to dispaly error message when text field fails validation.
|
||||
Method used to dispaly error message when field fails validation.
|
||||
|
||||
- returns: String of error message.
|
||||
*/
|
||||
|
|
@ -67,7 +67,7 @@ private protocol ISBNValidator {
|
|||
- returns: A `Bool` that represents what happened during verification. `false` is returned if
|
||||
it fails, `true` is returned if it was a success.
|
||||
*/
|
||||
func verify(input: String) -> Bool
|
||||
func verify(_ input: String) -> Bool
|
||||
/**
|
||||
Method that verifies regular expression is valid.
|
||||
|
||||
|
|
@ -75,7 +75,7 @@ private protocol ISBNValidator {
|
|||
- returns: A `Bool` that represents the status of the ISBN number. `false` if ISBN number
|
||||
was not valid, `true` if it was valid.
|
||||
*/
|
||||
func checkRegex(input: String) -> Bool
|
||||
func checkRegex(_ input: String) -> Bool
|
||||
|
||||
/**
|
||||
Method that verifies `ISBN` being validated is itself valid. It has to be either ISBN10
|
||||
|
|
@ -85,7 +85,7 @@ private protocol ISBNValidator {
|
|||
- returns: A `Bool` that represents the status of the ISBN number. `false` if ISBN number
|
||||
was not valid, `true` if it was valid.
|
||||
*/
|
||||
func verifyChecksum(input: String) -> Bool
|
||||
func verifyChecksum(_ input: String) -> Bool
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -100,7 +100,7 @@ extension ISBNValidator {
|
|||
- returns: A `Bool` that represents what happened during verification. `false` is returned if
|
||||
it fails, `true` is returned if it was a success.
|
||||
*/
|
||||
func verify(input: String) -> Bool {
|
||||
func verify(_ input: String) -> Bool {
|
||||
return checkRegex(input) && verifyChecksum(input)
|
||||
}
|
||||
|
||||
|
|
@ -112,8 +112,8 @@ extension ISBNValidator {
|
|||
- returns: A `Bool` that represents the status of the ISBN number. `false` if ISBN number
|
||||
was not valid, `true` if it was valid.
|
||||
*/
|
||||
func checkRegex(input: String) -> Bool {
|
||||
guard let _ = input.rangeOfString(regex, options: [.RegularExpressionSearch, .AnchoredSearch]) else {
|
||||
func checkRegex(_ input: String) -> Bool {
|
||||
guard let _ = input.range(of: regex, options: [.regularExpression, .anchored]) else {
|
||||
return false
|
||||
}
|
||||
|
||||
|
|
@ -136,19 +136,19 @@ private struct ISBN10Validator: ISBNValidator {
|
|||
- parameter input: String that is checked for ISBN10 validation.
|
||||
- returns: `true` if string is a valid ISBN10 and `false` if it is not.
|
||||
*/
|
||||
private func verifyChecksum(input: String) -> Bool {
|
||||
fileprivate func verifyChecksum(_ input: String) -> Bool {
|
||||
var checksum = 0
|
||||
|
||||
for i in 0..<9 {
|
||||
if let intCharacter = Int(String(input[input.startIndex.advancedBy(i)])) {
|
||||
if let intCharacter = Int(String(input[input.index(input.startIndex, offsetBy: i)])) {
|
||||
checksum += (i + 1) * intCharacter
|
||||
}
|
||||
}
|
||||
|
||||
if (input[input.startIndex.advancedBy(9)] == "X") {
|
||||
if (input[input.index(input.startIndex, offsetBy: 9)] == "X") {
|
||||
checksum += 10 * 10
|
||||
} else {
|
||||
if let intCharacter = Int(String(input[input.startIndex.advancedBy(9)])) {
|
||||
if let intCharacter = Int(String(input[input.index(input.startIndex, offsetBy: 9)])) {
|
||||
checksum += 10 * intCharacter
|
||||
}
|
||||
}
|
||||
|
|
@ -171,21 +171,21 @@ private struct ISBN13Validator: ISBNValidator {
|
|||
- parameter input: String that is checked for ISBN13 validation.
|
||||
- returns: `true` if string is a valid ISBN13 and `false` if it is not.
|
||||
*/
|
||||
private func verifyChecksum(input: String) -> Bool {
|
||||
fileprivate func verifyChecksum(_ input: String) -> Bool {
|
||||
let factor = [1, 3]
|
||||
var checksum = 0
|
||||
|
||||
for i in 0..<12 {
|
||||
if let intCharacter = Int(String(input[input.startIndex.advancedBy(i)])) {
|
||||
if let intCharacter = Int(String(input[input.index(input.startIndex, offsetBy: i)])) {
|
||||
print("\(factor[i%2]) * \(intCharacter)")
|
||||
checksum += factor[i % 2] * intCharacter
|
||||
}
|
||||
}
|
||||
|
||||
if let lastInt = Int(String(input[input.startIndex.advancedBy(12)])) {
|
||||
if let lastInt = Int(String(input[input.index(input.startIndex, offsetBy: 12)])) {
|
||||
return (lastInt - ((10 - (checksum % 10)) % 10) == 0)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ public class MaxLengthRule: Rule {
|
|||
public init(){}
|
||||
|
||||
/**
|
||||
Initializes a `MaxLengthRule` object that is to validate the length of the text of a text field.
|
||||
Initializes a `MaxLengthRule` object that is to validate the length of the text of a field.
|
||||
|
||||
- parameter length: Maximum character length.
|
||||
- parameter message: String of error message.
|
||||
|
|
@ -27,21 +27,21 @@ public class MaxLengthRule: Rule {
|
|||
*/
|
||||
public init(length: Int, message : String = "Must be at most %ld characters long"){
|
||||
self.DEFAULT_LENGTH = length
|
||||
self.message = NSString(format: message, self.DEFAULT_LENGTH) as String
|
||||
self.message = String(format: message, self.DEFAULT_LENGTH)
|
||||
}
|
||||
|
||||
/**
|
||||
Used to validate a text field.
|
||||
Used to validate a field.
|
||||
|
||||
- parameter value: String to checked for validation.
|
||||
- returns: A boolean value. True if validation is successful; False if validation fails.
|
||||
*/
|
||||
public func validate(value: String) -> Bool {
|
||||
return value.characters.count <= DEFAULT_LENGTH
|
||||
public func validate(_ value: String) -> Bool {
|
||||
return value.count <= DEFAULT_LENGTH
|
||||
}
|
||||
|
||||
/**
|
||||
Displays an error message if a text field fails validation.
|
||||
Displays an error message if a field fails validation.
|
||||
|
||||
- returns: String of error message.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ public class MinLengthRule: Rule {
|
|||
public init(){}
|
||||
|
||||
/**
|
||||
Initializes a `MaxLengthRule` object that is to validate the length of the text of a text field.
|
||||
Initializes a `MaxLengthRule` object that is to validate the length of the text of a field.
|
||||
|
||||
- parameter length: Minimum character length.
|
||||
- parameter message: String of error message.
|
||||
|
|
@ -29,20 +29,20 @@ public class MinLengthRule: Rule {
|
|||
*/
|
||||
public init(length: Int, message : String = "Must be at least %ld characters long"){
|
||||
self.DEFAULT_LENGTH = length
|
||||
self.message = NSString(format: message, self.DEFAULT_LENGTH) as String
|
||||
self.message = String(format: message, self.DEFAULT_LENGTH)
|
||||
}
|
||||
|
||||
/**
|
||||
Validates a text field.
|
||||
Validates a field.
|
||||
- parameter value: String to checked for validation.
|
||||
- returns: A boolean value. True if validation is successful; False if validation fails.
|
||||
*/
|
||||
public func validate(value: String) -> Bool {
|
||||
return value.characters.count >= DEFAULT_LENGTH
|
||||
public func validate(_ value: String) -> Bool {
|
||||
return value.count >= DEFAULT_LENGTH
|
||||
}
|
||||
|
||||
/**
|
||||
Displays error message when text field has failed validation.
|
||||
Displays error message when field has failed validation.
|
||||
|
||||
- returns: String of error message.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ public class PasswordRule : RegexRule {
|
|||
static let regex = "^(?=.*?[A-Z]).{8,}$"
|
||||
|
||||
/**
|
||||
Initializes a `PasswordRule` object that will validate a text field is a valid password.
|
||||
Initializes a `PasswordRule` object that will validate a field is a valid password.
|
||||
|
||||
- parameter message: String of error message.
|
||||
- returns: An initialized `PasswordRule` object, or nil if an object could not be created for some reason that would not result in an exception.
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import Foundation
|
|||
/**
|
||||
`RegexRule` is a subclass of Rule that defines how a regular expression is validated.
|
||||
*/
|
||||
public class RegexRule : Rule {
|
||||
open class RegexRule : Rule {
|
||||
/// Regular express string to be used in validation.
|
||||
private var REGEX: String = "^(?=.*?[A-Z]).{8,}$"
|
||||
/// String that holds error message.
|
||||
|
|
@ -30,22 +30,22 @@ public class RegexRule : Rule {
|
|||
}
|
||||
|
||||
/**
|
||||
Method used to validate text field.
|
||||
Method used to validate field.
|
||||
|
||||
- parameter value: String to checked for validation.
|
||||
- returns: Boolean value. True if validation is successful; False if validation fails.
|
||||
*/
|
||||
public func validate(value: String) -> Bool {
|
||||
open func validate(_ value: String) -> Bool {
|
||||
let test = NSPredicate(format: "SELF MATCHES %@", self.REGEX)
|
||||
return test.evaluateWithObject(value)
|
||||
return test.evaluate(with: value)
|
||||
}
|
||||
|
||||
/**
|
||||
Method used to dispaly error message when text field fails validation.
|
||||
Method used to dispaly error message when field fails validation.
|
||||
|
||||
- returns: String of error message.
|
||||
*/
|
||||
public func errorMessage() -> String {
|
||||
open func errorMessage() -> String {
|
||||
return message
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,9 +9,9 @@
|
|||
import Foundation
|
||||
|
||||
/**
|
||||
`RequiredRule` is a subclass of Rule that defines how a required text field is validated.
|
||||
`RequiredRule` is a subclass of Rule that defines how a required field is validated.
|
||||
*/
|
||||
public class RequiredRule: Rule {
|
||||
open class RequiredRule: Rule {
|
||||
/// String that holds error message.
|
||||
private var message : String
|
||||
|
||||
|
|
@ -26,12 +26,12 @@ public class RequiredRule: Rule {
|
|||
}
|
||||
|
||||
/**
|
||||
Validates a text field.
|
||||
Validates a field.
|
||||
|
||||
- parameter value: String to checked for validation.
|
||||
- returns: Boolean value. True if validation is successful; False if validation fails.
|
||||
*/
|
||||
public func validate(value: String) -> Bool {
|
||||
open func validate(_ value: String) -> Bool {
|
||||
return !value.isEmpty
|
||||
}
|
||||
|
||||
|
|
@ -40,7 +40,7 @@ public class RequiredRule: Rule {
|
|||
|
||||
- returns: String of error message.
|
||||
*/
|
||||
public func errorMessage() -> String {
|
||||
open func errorMessage() -> String {
|
||||
return message
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,14 +12,14 @@ import Foundation
|
|||
*/
|
||||
public protocol Rule {
|
||||
/**
|
||||
Validates text of a text field.
|
||||
Validates text of a field.
|
||||
|
||||
- parameter value: String of text to be validated.
|
||||
- returns: Boolean value. True if validation is successful; False if validation fails.
|
||||
*/
|
||||
func validate(value: String) -> Bool
|
||||
func validate(_ value: String) -> Bool
|
||||
/**
|
||||
Displays error message of a text field that has failed validation.
|
||||
Displays error message of a field that has failed validation.
|
||||
|
||||
- returns: String of error message.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -9,36 +9,37 @@ import Foundation
|
|||
import UIKit
|
||||
|
||||
/**
|
||||
`ValidationRule` is a class that creates an object which holds validation info of a text field.
|
||||
`ValidationRule` is a class that creates an object which holds validation info of a field.
|
||||
*/
|
||||
public class ValidationRule {
|
||||
/// the text field of the field
|
||||
public var textField:UITextField
|
||||
/// the field of the field
|
||||
public var field:ValidatableField
|
||||
/// the errorLabel of the field
|
||||
public var errorLabel:UILabel?
|
||||
/// the rules of the field
|
||||
public var rules:[Rule] = []
|
||||
|
||||
/**
|
||||
Initializes `ValidationRule` instance with text field, rules, and errorLabel.
|
||||
Initializes `ValidationRule` instance with field, rules, and errorLabel.
|
||||
|
||||
- parameter textField: text field that holds actual text in text field.
|
||||
- parameter errorLabel: label that holds error label of text field.
|
||||
- parameter rules: array of Rule objects, which text field will be validated against.
|
||||
- parameter field: field that holds actual text in field.
|
||||
- parameter errorLabel: label that holds error label of field.
|
||||
- parameter rules: array of Rule objects, which field will be validated against.
|
||||
- returns: An initialized `ValidationRule` object, or nil if an object could not be created for some reason that would not result in an exception.
|
||||
*/
|
||||
public init(textField: UITextField, rules:[Rule], errorLabel:UILabel?){
|
||||
self.textField = textField
|
||||
public init(field: ValidatableField, rules:[Rule], errorLabel:UILabel?){
|
||||
self.field = field
|
||||
self.errorLabel = errorLabel
|
||||
self.rules = rules
|
||||
}
|
||||
|
||||
/**
|
||||
Used to validate text field against its validation rules.
|
||||
Used to validate field against its validation rules.
|
||||
- returns: `ValidationError` object if at least one error is found. Nil is returned if there are no validation errors.
|
||||
*/
|
||||
public func validateField() -> ValidationError? {
|
||||
return rules.filter{ !$0.validate(self.textField.text ?? "") }
|
||||
.map{ rule -> ValidationError in return ValidationError(textField: self.textField, errorLabel:self.errorLabel, error: rule.errorMessage()) }.first
|
||||
return rules.filter{
|
||||
return !$0.validate(field.validationText)
|
||||
}.map{ rule -> ValidationError in return ValidationError(field: self.field, errorLabel:self.errorLabel, error: rule.errorMessage()) }.first
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -366,7 +366,7 @@ class SwiftValidatorTests: XCTestCase {
|
|||
}
|
||||
REGISTER_TXT_FIELD.text = INVALID_EMAIL
|
||||
REGISTER_VALIDATOR.validateField(REGISTER_TXT_FIELD) { error in
|
||||
XCTAssert(error?.errorMessage.characters.count > 0, "Should state 'invalid email'")
|
||||
XCTAssert(error?.errorMessage.count ?? 0 > 0, "Should state 'invalid email'")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -385,9 +385,9 @@ class SwiftValidatorTests: XCTestCase {
|
|||
var successCount = 0
|
||||
var errorCount = 0
|
||||
REGISTER_VALIDATOR.styleTransformers(success: { (validationRule) -> Void in
|
||||
successCount++
|
||||
successCount+=1
|
||||
}) { (validationError) -> Void in
|
||||
errorCount++
|
||||
errorCount+=1
|
||||
}
|
||||
REGISTER_TXT_FIELD.text = INVALID_EMAIL
|
||||
REGISTER_VALIDATOR.validate { (errors) -> Void in
|
||||
|
|
@ -403,9 +403,9 @@ class SwiftValidatorTests: XCTestCase {
|
|||
var successCount = 0
|
||||
var errorCount = 0
|
||||
REGISTER_VALIDATOR.styleTransformers(success: { (validationRule) -> Void in
|
||||
successCount++
|
||||
successCount+=1
|
||||
}) { (validationError) -> Void in
|
||||
errorCount++
|
||||
errorCount+=1
|
||||
}
|
||||
|
||||
REGISTER_TXT_FIELD.text = INVALID_EMAIL
|
||||
|
|
@ -427,7 +427,7 @@ class SwiftValidatorTests: XCTestCase {
|
|||
REGISTER_TXT_FIELD.text = INVALID_EMAIL
|
||||
REGISTER_VALIDATOR.validate { (errors) -> Void in
|
||||
XCTAssert(errors.count == 1, "Should come back with errors")
|
||||
XCTAssert(!CGColorEqualToColor(self.REGISTER_TXT_FIELD.layer.borderColor, UIColor.redColor().CGColor), "Color shouldn't get set at all")
|
||||
XCTAssert(!(self.REGISTER_TXT_FIELD.layer.borderColor! == UIColor.red.cgColor), "Color shouldn't get set at all")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@
|
|||
62D1AE221A1E6D4400E4DFF8 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 62D1AE201A1E6D4400E4DFF8 /* Main.storyboard */; };
|
||||
62D1AE241A1E6D4400E4DFF8 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 62D1AE231A1E6D4400E4DFF8 /* Images.xcassets */; };
|
||||
62D1AE271A1E6D4400E4DFF8 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 62D1AE251A1E6D4400E4DFF8 /* LaunchScreen.xib */; };
|
||||
62D9B2561C7C0B2A00BAFCE3 /* ValidationDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62D9B2551C7C0B2A00BAFCE3 /* ValidationDelegate.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
62D9B2561C7C0B2A00BAFCE3 /* ValidationDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62D9B2551C7C0B2A00BAFCE3 /* ValidationDelegate.swift */; };
|
||||
7CC1E4CF1C636B4500AF013C /* AlphaRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CC1E4CE1C636B4500AF013C /* AlphaRule.swift */; };
|
||||
7CC1E4D11C637A7700AF013C /* AlphaNumericRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CC1E4D01C637A7700AF013C /* AlphaNumericRule.swift */; };
|
||||
7CC1E4D31C637ABC00AF013C /* CharacterSetRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CC1E4D21C637ABC00AF013C /* CharacterSetRule.swift */; };
|
||||
|
|
@ -40,6 +40,8 @@
|
|||
FB465CFF1B9889EA00398388 /* ZipCodeRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB465CEF1B9889EA00398388 /* ZipCodeRule.swift */; };
|
||||
FB465D001B9889EA00398388 /* ValidationError.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB465CF11B9889EA00398388 /* ValidationError.swift */; };
|
||||
FB465D011B9889EA00398388 /* Validator.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB465CF21B9889EA00398388 /* Validator.swift */; };
|
||||
FB51E5B01CD208B8004DE696 /* Validatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB51E5AF1CD208B8004DE696 /* Validatable.swift */; };
|
||||
FBA963631CDA10130071F03E /* ValidatorDictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = FBA963621CDA10130071F03E /* ValidatorDictionary.swift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXContainerItemProxy section */
|
||||
|
|
@ -126,6 +128,8 @@
|
|||
FB465CEF1B9889EA00398388 /* ZipCodeRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ZipCodeRule.swift; sourceTree = "<group>"; };
|
||||
FB465CF11B9889EA00398388 /* ValidationError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValidationError.swift; sourceTree = "<group>"; };
|
||||
FB465CF21B9889EA00398388 /* Validator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Validator.swift; sourceTree = "<group>"; };
|
||||
FB51E5AF1CD208B8004DE696 /* Validatable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Validatable.swift; sourceTree = "<group>"; };
|
||||
FBA963621CDA10130071F03E /* ValidatorDictionary.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValidatorDictionary.swift; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
|
|
@ -290,6 +294,8 @@
|
|||
62D9B2551C7C0B2A00BAFCE3 /* ValidationDelegate.swift */,
|
||||
FB465CF11B9889EA00398388 /* ValidationError.swift */,
|
||||
FB465CF21B9889EA00398388 /* Validator.swift */,
|
||||
FB51E5AF1CD208B8004DE696 /* Validatable.swift */,
|
||||
FBA963621CDA10130071F03E /* ValidatorDictionary.swift */,
|
||||
);
|
||||
path = Core;
|
||||
sourceTree = "<group>";
|
||||
|
|
@ -390,21 +396,25 @@
|
|||
attributes = {
|
||||
LastSwiftMigration = 0700;
|
||||
LastSwiftUpdateCheck = 0700;
|
||||
LastUpgradeCheck = 0700;
|
||||
LastUpgradeCheck = 0930;
|
||||
ORGANIZATIONNAME = jpotts18;
|
||||
TargetAttributes = {
|
||||
62D1AE161A1E6D4400E4DFF8 = {
|
||||
CreatedOnToolsVersion = 6.1;
|
||||
LastSwiftMigration = 0800;
|
||||
};
|
||||
62D1AE2B1A1E6D4500E4DFF8 = {
|
||||
CreatedOnToolsVersion = 6.1;
|
||||
LastSwiftMigration = 0800;
|
||||
TestTargetID = 62D1AE161A1E6D4400E4DFF8;
|
||||
};
|
||||
FB465CB21B9884F400398388 = {
|
||||
CreatedOnToolsVersion = 6.4;
|
||||
LastSwiftMigration = 1010;
|
||||
};
|
||||
FB465CBC1B9884F400398388 = {
|
||||
CreatedOnToolsVersion = 6.4;
|
||||
LastSwiftMigration = 1010;
|
||||
TestTargetID = 62D1AE161A1E6D4400E4DFF8;
|
||||
};
|
||||
};
|
||||
|
|
@ -487,6 +497,7 @@
|
|||
files = (
|
||||
FB465CF41B9889EA00398388 /* EmailRule.swift in Sources */,
|
||||
FB465CF61B9889EA00398388 /* FullNameRule.swift in Sources */,
|
||||
FBA963631CDA10130071F03E /* ValidatorDictionary.swift in Sources */,
|
||||
FB465CFF1B9889EA00398388 /* ZipCodeRule.swift in Sources */,
|
||||
FB465CF91B9889EA00398388 /* PasswordRule.swift in Sources */,
|
||||
7CC1E4D11C637A7700AF013C /* AlphaNumericRule.swift in Sources */,
|
||||
|
|
@ -498,6 +509,7 @@
|
|||
FB465D011B9889EA00398388 /* Validator.swift in Sources */,
|
||||
FB465CFE1B9889EA00398388 /* ValidationRule.swift in Sources */,
|
||||
FB465CF31B9889EA00398388 /* ConfirmRule.swift in Sources */,
|
||||
FB51E5B01CD208B8004DE696 /* Validatable.swift in Sources */,
|
||||
7CC1E4D51C637C8500AF013C /* IPV4Rule.swift in Sources */,
|
||||
7CC1E4D71C637F6E00AF013C /* ISBNRule.swift in Sources */,
|
||||
FB465D001B9889EA00398388 /* ValidationError.swift in Sources */,
|
||||
|
|
@ -572,13 +584,23 @@
|
|||
CLANG_CXX_LIBRARY = "libc++";
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CLANG_ENABLE_OBJC_ARC = YES;
|
||||
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
|
||||
CLANG_WARN_BOOL_CONVERSION = YES;
|
||||
CLANG_WARN_COMMA = YES;
|
||||
CLANG_WARN_CONSTANT_CONVERSION = YES;
|
||||
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
|
||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
|
||||
CLANG_WARN_EMPTY_BODY = YES;
|
||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
||||
CLANG_WARN_INFINITE_RECURSION = YES;
|
||||
CLANG_WARN_INT_CONVERSION = YES;
|
||||
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
|
||||
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
||||
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
|
||||
CLANG_WARN_STRICT_PROTOTYPES = YES;
|
||||
CLANG_WARN_SUSPICIOUS_MOVE = YES;
|
||||
CLANG_WARN_UNREACHABLE_CODE = YES;
|
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
|
||||
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
|
||||
|
|
@ -587,6 +609,7 @@
|
|||
ENABLE_TESTABILITY = YES;
|
||||
GCC_C_LANGUAGE_STANDARD = gnu99;
|
||||
GCC_DYNAMIC_NO_PIC = NO;
|
||||
GCC_NO_COMMON_BLOCKS = YES;
|
||||
GCC_OPTIMIZATION_LEVEL = 0;
|
||||
GCC_PREPROCESSOR_DEFINITIONS = (
|
||||
"DEBUG=1",
|
||||
|
|
@ -604,6 +627,7 @@
|
|||
ONLY_ACTIVE_ARCH = YES;
|
||||
SDKROOT = iphoneos;
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
||||
SWIFT_VERSION = 4.0;
|
||||
};
|
||||
name = Debug;
|
||||
};
|
||||
|
|
@ -615,13 +639,23 @@
|
|||
CLANG_CXX_LIBRARY = "libc++";
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CLANG_ENABLE_OBJC_ARC = YES;
|
||||
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
|
||||
CLANG_WARN_BOOL_CONVERSION = YES;
|
||||
CLANG_WARN_COMMA = YES;
|
||||
CLANG_WARN_CONSTANT_CONVERSION = YES;
|
||||
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
|
||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
|
||||
CLANG_WARN_EMPTY_BODY = YES;
|
||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
||||
CLANG_WARN_INFINITE_RECURSION = YES;
|
||||
CLANG_WARN_INT_CONVERSION = YES;
|
||||
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
|
||||
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
||||
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
|
||||
CLANG_WARN_STRICT_PROTOTYPES = YES;
|
||||
CLANG_WARN_SUSPICIOUS_MOVE = YES;
|
||||
CLANG_WARN_UNREACHABLE_CODE = YES;
|
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
|
||||
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
|
||||
|
|
@ -629,6 +663,7 @@
|
|||
ENABLE_NS_ASSERTIONS = NO;
|
||||
ENABLE_STRICT_OBJC_MSGSEND = YES;
|
||||
GCC_C_LANGUAGE_STANDARD = gnu99;
|
||||
GCC_NO_COMMON_BLOCKS = YES;
|
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
||||
GCC_WARN_UNDECLARED_SELECTOR = YES;
|
||||
|
|
@ -638,6 +673,8 @@
|
|||
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
|
||||
MTL_ENABLE_DEBUG_INFO = NO;
|
||||
SDKROOT = iphoneos;
|
||||
SWIFT_COMPILATION_MODE = wholemodule;
|
||||
SWIFT_VERSION = 4.0;
|
||||
VALIDATE_PRODUCT = YES;
|
||||
};
|
||||
name = Release;
|
||||
|
|
@ -650,6 +687,7 @@
|
|||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "me.jeffpotter.$(PRODUCT_NAME:rfc1034identifier)";
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_VERSION = 4.2;
|
||||
};
|
||||
name = Debug;
|
||||
};
|
||||
|
|
@ -661,6 +699,8 @@
|
|||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "me.jeffpotter.$(PRODUCT_NAME:rfc1034identifier)";
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
|
||||
SWIFT_VERSION = 4.2;
|
||||
};
|
||||
name = Release;
|
||||
};
|
||||
|
|
@ -676,6 +716,7 @@
|
|||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "me.jeffpotter.$(PRODUCT_NAME:rfc1034identifier)";
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_VERSION = 4.1;
|
||||
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Validator.app/Validator";
|
||||
};
|
||||
name = Debug;
|
||||
|
|
@ -688,6 +729,7 @@
|
|||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "me.jeffpotter.$(PRODUCT_NAME:rfc1034identifier)";
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_VERSION = 4.1;
|
||||
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Validator.app/Validator";
|
||||
};
|
||||
name = Release;
|
||||
|
|
@ -695,6 +737,7 @@
|
|||
FB465CCC1B9884F400398388 /* Debug */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
CODE_SIGN_IDENTITY = "";
|
||||
CURRENT_PROJECT_VERSION = 1;
|
||||
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
|
||||
DEFINES_MODULE = YES;
|
||||
|
|
@ -712,6 +755,7 @@
|
|||
PRODUCT_BUNDLE_IDENTIFIER = "me.jeffpotter.$(PRODUCT_NAME:rfc1034identifier)";
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SKIP_INSTALL = YES;
|
||||
SWIFT_VERSION = 4.2;
|
||||
TARGETED_DEVICE_FAMILY = "1,2";
|
||||
VERSIONING_SYSTEM = "apple-generic";
|
||||
VERSION_INFO_PREFIX = "";
|
||||
|
|
@ -721,6 +765,7 @@
|
|||
FB465CCD1B9884F400398388 /* Release */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
CODE_SIGN_IDENTITY = "";
|
||||
COPY_PHASE_STRIP = NO;
|
||||
CURRENT_PROJECT_VERSION = 1;
|
||||
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
|
||||
|
|
@ -735,6 +780,8 @@
|
|||
PRODUCT_BUNDLE_IDENTIFIER = "me.jeffpotter.$(PRODUCT_NAME:rfc1034identifier)";
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SKIP_INSTALL = YES;
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
|
||||
SWIFT_VERSION = 4.2;
|
||||
TARGETED_DEVICE_FAMILY = "1,2";
|
||||
VERSIONING_SYSTEM = "apple-generic";
|
||||
VERSION_INFO_PREFIX = "";
|
||||
|
|
@ -759,6 +806,7 @@
|
|||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.levous.$(PRODUCT_NAME:rfc1034identifier)";
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_VERSION = 4.2;
|
||||
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Validator.app/Validator";
|
||||
};
|
||||
name = Debug;
|
||||
|
|
@ -778,6 +826,8 @@
|
|||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.levous.$(PRODUCT_NAME:rfc1034identifier)";
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
|
||||
SWIFT_VERSION = 4.2;
|
||||
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Validator.app/Validator";
|
||||
};
|
||||
name = Release;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>IDEDidComputeMac32BitWarning</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</plist>
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme
|
||||
LastUpgradeVersion = "0700"
|
||||
LastUpgradeVersion = "0930"
|
||||
version = "1.3">
|
||||
<BuildAction
|
||||
parallelizeBuildables = "YES"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme
|
||||
LastUpgradeVersion = "0700"
|
||||
LastUpgradeVersion = "0930"
|
||||
version = "1.3">
|
||||
<BuildAction
|
||||
parallelizeBuildables = "YES"
|
||||
|
|
@ -26,8 +26,8 @@
|
|||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES"
|
||||
codeCoverageEnabled = "YES">
|
||||
codeCoverageEnabled = "YES"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES">
|
||||
<Testables>
|
||||
<TestableReference
|
||||
skipped = "NO">
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme
|
||||
LastUpgradeVersion = "0700"
|
||||
LastUpgradeVersion = "0930"
|
||||
version = "1.3">
|
||||
<BuildAction
|
||||
parallelizeBuildables = "YES"
|
||||
|
|
|
|||
|
|
@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
|
|||
var window: UIWindow?
|
||||
|
||||
|
||||
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
|
||||
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
|
||||
// Override point for customization after application launch.
|
||||
return true
|
||||
}
|
||||
|
||||
func applicationWillResignActive(application: UIApplication) {
|
||||
func applicationWillResignActive(_ application: UIApplication) {
|
||||
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
|
||||
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
|
||||
}
|
||||
|
||||
func applicationDidEnterBackground(application: UIApplication) {
|
||||
func applicationDidEnterBackground(_ application: UIApplication) {
|
||||
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
|
||||
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
|
||||
}
|
||||
|
||||
func applicationWillEnterForeground(application: UIApplication) {
|
||||
func applicationWillEnterForeground(_ application: UIApplication) {
|
||||
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
|
||||
}
|
||||
|
||||
func applicationDidBecomeActive(application: UIApplication) {
|
||||
func applicationDidBecomeActive(_ application: UIApplication) {
|
||||
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
|
||||
}
|
||||
|
||||
func applicationWillTerminate(application: UIApplication) {
|
||||
func applicationWillTerminate(_ application: UIApplication) {
|
||||
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -31,22 +31,26 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
|
|||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "hideKeyboard"))
|
||||
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(ViewController.hideKeyboard)))
|
||||
|
||||
validator.styleTransformers(success:{ (validationRule) -> Void in
|
||||
print("here")
|
||||
// clear error label
|
||||
validationRule.errorLabel?.hidden = true
|
||||
validationRule.errorLabel?.text = ""
|
||||
validationRule.textField.layer.borderColor = UIColor.greenColor().CGColor
|
||||
validationRule.textField.layer.borderWidth = 0.5
|
||||
|
||||
}, error:{ (validationError) -> Void in
|
||||
print("error")
|
||||
validationError.errorLabel?.hidden = false
|
||||
validationError.errorLabel?.text = validationError.errorMessage
|
||||
validationError.textField.layer.borderColor = UIColor.redColor().CGColor
|
||||
validationError.textField.layer.borderWidth = 1.0
|
||||
// clear error label
|
||||
validationRule.errorLabel?.isHidden = true
|
||||
validationRule.errorLabel?.text = ""
|
||||
if let textField = validationRule.field as? UITextField {
|
||||
textField.layer.borderColor = UIColor.green.cgColor
|
||||
textField.layer.borderWidth = 0.5
|
||||
|
||||
}
|
||||
}, error:{ (validationError) -> Void in
|
||||
print("error")
|
||||
validationError.errorLabel?.isHidden = false
|
||||
validationError.errorLabel?.text = validationError.errorMessage
|
||||
if let textField = validationError.field as? UITextField {
|
||||
textField.layer.borderColor = UIColor.red.cgColor
|
||||
textField.layer.borderWidth = 1.0
|
||||
}
|
||||
})
|
||||
|
||||
validator.registerField(fullNameTextField, errorLabel: fullNameErrorLabel , rules: [RequiredRule(), FullNameRule()])
|
||||
|
|
@ -56,7 +60,7 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
|
|||
validator.registerField(zipcodeTextField, errorLabel: zipcodeErrorLabel, rules: [RequiredRule(), ZipCodeRule()])
|
||||
}
|
||||
|
||||
@IBAction func submitTapped(sender: AnyObject) {
|
||||
@IBAction func submitTapped(_ sender: AnyObject) {
|
||||
print("Validating...")
|
||||
validator.validate(self)
|
||||
}
|
||||
|
|
@ -65,23 +69,23 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
|
|||
|
||||
func validationSuccessful() {
|
||||
print("Validation Success!")
|
||||
let alert = UIAlertController(title: "Success", message: "You are validated!", preferredStyle: UIAlertControllerStyle.Alert)
|
||||
let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
|
||||
let alert = UIAlertController(title: "Success", message: "You are validated!", preferredStyle: UIAlertController.Style.alert)
|
||||
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
|
||||
alert.addAction(defaultAction)
|
||||
self.presentViewController(alert, animated: true, completion: nil)
|
||||
self.present(alert, animated: true, completion: nil)
|
||||
|
||||
}
|
||||
func validationFailed(errors:[UITextField:ValidationError]) {
|
||||
func validationFailed(_ errors:[(Validatable, ValidationError)]) {
|
||||
print("Validation FAILED!")
|
||||
}
|
||||
|
||||
func hideKeyboard(){
|
||||
@objc func hideKeyboard(){
|
||||
self.view.endEditing(true)
|
||||
}
|
||||
|
||||
// MARK: Validate single field
|
||||
// Don't forget to use UITextFieldDelegate
|
||||
func textFieldShouldReturn(textField: UITextField) -> Bool {
|
||||
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
|
||||
validator.validateField(textField){ error in
|
||||
if error == nil {
|
||||
// Field validation was successful
|
||||
|
|
@ -92,4 +96,4 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
|
|||
return true
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue