swiftreactive-cocoa-5

Form validation in reactive cocoa


I am trying to validate a form which has multiple textfields. But I can observe only when both textfields are edited...

let validUserNameSignal =
    self.nameTextField
        .reactive
        .continuousTextValues
        .skipNil()
        .map({ $0.characters.count > 3 })

let pwdPasswordFieldSignal = 
    self.lastnameTextField.reactive
        .continuousTextValues
        .skipNil()
        .map({$0.characters.count > 3})

let formValidation =  validUserNameSignal.combineLatest(with: pwdPasswordFieldSignal)

formValidation.observeValues { (userNameResult,pwdResult) in
    print(userNameResult)
    print(pwdResult)
}

Is the way I am doing ok or there is another way?


Solution

  • Here's an example implementation of a basic ViewController, I've cleaned up a few of the things that I think could be done better.

    class ViewController: UIViewController {
        @IBOutlet weak var nameTextField: UITextField!
        @IBOutlet weak var passwordTextField: UITextField!
    
        func nameValidation(for field: UITextField) -> Signal<Bool, NoError> {
            return field
                .reactive
                .continuousTextValues
                .skipNil()
                .map { $0.characters.count > 3 }
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let validUserNameSignal = nameValidation(for: nameTextField)
            let lastNameFieldSignal = nameValidation(for: lastNameTextField)
            let formValidation =
                SignalProducer(signal: validUserNameSignal.combineLatest(with: lastNameFieldSignal))
                    .map { $0 && $1 }
                    .prefix(value: false)
    
            formValidation.startWithValues {
                print($0)
            }
        }
    }