简体   繁体   中英

Swift - How to use integrate SwiftValidator into my project

I've got a login form which takes 2 alternate forms dependent on how a user taps a segmented control .... either as a register form with email, password, and repeat password; or as a login form with just email and password.

When a user fills out the register or login fields and taps the register/login button ... I log the user in using code along the lines of ...

    @IBAction func loginRegisterTapped(sender: AnyObject) {
       //check whether the sender tag is registerTag (in which case a new user is registering or alternately returning user logging in 
       //do various checks and if all good submit the form
}

Ive recently copied over a validation library (SwiftValidator) to my project, and configured it to also validate fields when the login/register button is tapped ie i insert the validation code above my own code in the loginRegisterTapped ibaction method. My code after integrating SwiftValidator essentially becomes

@IBAction func loginRegisterTapped(sender: AnyObject) {
       //validation
    self.clearErrors()
    validator.validate(self)
       //check whether the sender tag is registerTag (in which case a new user is registering or alternately returning user logging in 
       //do various checks and if all good submit the form
}

The SwiftValidator library ends us calling 2 delegate methods (in my LoginViewController) on validation success or failure as follows

    // SwiftValidator library Delegate methods
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
    }
}

Given validation is successful, how can i get code to return to my @IBAction func loginRegisterTapped(sender: AnyObject) method and continue with my own form submission code (... I need a reference to the sender to check it's tag to determine whether the button was tapped as a "Register" button or a "Login" button) rather than conducting the form submission as suggested by the SwiftValidator library within it's validationSuccessful delegate method where i dont have access to the sender var. ie what options do i have here or what would be considered best practice? ... I thought of customizing the validationSuccessful method to return perhaps a bool to the calling function but doubt if that's the best way?

IMHO, I'd do the successful form submission in the validationSuccessful() method, not in your own action method. I'd avoid using tags to differentiate the login and register buttons, and instead have each one call a separate action method. You're not saving any time, code, or logical clarity by handling them both with the same handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM