Search code examples
iosswiftvalidationmobile

Validating Form Field in IOS


I have imported as below:

import SwiftValidator

Below is my ViewController class:

class OriginShipmentViewController: BaseViewController, OriginShipmentView {
     let validator = Validator()
}

You can see that I have initialised validator

Below is the button save click code:

buttonSave?.configureAppearance(
        appearance.mainButtonAppearance, title: localizedKey(key: "origin.next"))
    buttonSave?.addTargetClosure(closure: {_ in
        self.onclickSave()
    })

So you can notice ther is a method call named self.onclickSave() which is as below:

func onclickSave() {
    validator.validate(self)
}

Here, on this line : validator.validate(self) I am getting compile time error as below:

No exact matches in call to instance method 'validate'

What might be the issue? Thanks in advance.


Solution

  • You have not conformed ValidationDelegate yet. By default, this protocol requires two functions validationSuccessful and validationFailed. Try to put these lines of code:

    extension OriginShipmentViewController: ValidationDelegate {
        func validationSuccessful() {
            //TODO: 
        }
        
        func validationFailed(_ errors: [(Validatable, ValidationError)]) {
            //TODO: 
        }
    }