SwiftValidator - OnValidationSuccessful
SwiftValidator - OnValidationSuccessful
我是 Swift 语言的新手。我正在尝试使用来自 @jpotts18 的 SwiftValidator 库。我必须使用规则并验证 UITextFields,但如果它们是正确的,我无法将它们恢复到正常状态。
这是我的视图控制器
class SignUpViewController: UIViewController, ValidationDelegate {
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var rePasswordTextField: UITextField!
let validator = Validator()
override func viewDidLoad() {
super.viewDidLoad()
validator.registerField(nameTextField, rules: [RequiredRule(), FullNameRule()])
}
func validationSuccessful() {
print("success")
}
func validationFailed(errors: [(Validatable, ValidationError)]) {
for (field, _) in errors {
if let field = field as? UITextField {
field.layer.borderColor = UIColor.redColor().CGColor
field.layer.borderWidth = 1.0
}
}
}
@IBAction func onRegisterClick(sender: UIButton) {
validator.validate(self)
}
}
如果验证失败,我的视图会变成红色,但如果没有,我不知道应该使用什么代码来 return 将特定的 UITextField 变为正常状态。
有什么建议吗?
谢谢。
经过反复试验,我设法用这段代码完成了我想做的事情。
validator.styleTransformers(success: {
(validationRule) -> Void in
print("Field Validated")
if let textField = validationRule.field as? UITextField {
textField.textColor = UIColor.blackColor()
}
}, error: {
(validationError) -> Void in
print("Field Incorrect")
if let textField = validationError.field as? UITextField {
textField.textColor = UIColor.redColor()
}
})
我是 Swift 语言的新手。我正在尝试使用来自 @jpotts18 的 SwiftValidator 库。我必须使用规则并验证 UITextFields,但如果它们是正确的,我无法将它们恢复到正常状态。
这是我的视图控制器
class SignUpViewController: UIViewController, ValidationDelegate {
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var rePasswordTextField: UITextField!
let validator = Validator()
override func viewDidLoad() {
super.viewDidLoad()
validator.registerField(nameTextField, rules: [RequiredRule(), FullNameRule()])
}
func validationSuccessful() {
print("success")
}
func validationFailed(errors: [(Validatable, ValidationError)]) {
for (field, _) in errors {
if let field = field as? UITextField {
field.layer.borderColor = UIColor.redColor().CGColor
field.layer.borderWidth = 1.0
}
}
}
@IBAction func onRegisterClick(sender: UIButton) {
validator.validate(self)
}
}
如果验证失败,我的视图会变成红色,但如果没有,我不知道应该使用什么代码来 return 将特定的 UITextField 变为正常状态。
有什么建议吗?
谢谢。
经过反复试验,我设法用这段代码完成了我想做的事情。
validator.styleTransformers(success: {
(validationRule) -> Void in
print("Field Validated")
if let textField = validationRule.field as? UITextField {
textField.textColor = UIColor.blackColor()
}
}, error: {
(validationError) -> Void in
print("Field Incorrect")
if let textField = validationError.field as? UITextField {
textField.textColor = UIColor.redColor()
}
})