Nuno Matos
Nuno Matos

Reputation: 29

SwiftValidator - OnValidationSuccessful

I am new to Swift language. I'm trying to use SwiftValidator library from @jpotts18. I got to use rules and validate UITextFields but I can't get them to normal state if they're correct.

This is my view controller

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)
     }
}

My view turns red if validation fails but if it doesn't I don't know what code should I use to return that specific UITextField to normal state.

Any suggestions?

Thanks.

Upvotes: 0

Views: 575

Answers (1)

Nuno Matos
Nuno Matos

Reputation: 29

After some trial and error I managed to do what I want with this code.

        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()
              }
        })

Upvotes: 2

Related Questions