Je dois valider le formulaire avec un bouton radio, mais je n'y arrive pas, je partage mon code :
Mon point de vue :
private func registerForm() {
tvUserName.rx.text.map { $0 ?? "" }.bind(to: viewModel.userNamePublishSubject).disposed(by: disposeBag)
tvEmail.rx.text.map { $0 ?? "" }.bind(to: viewModel.emailPublishSubject).disposed(by: disposeBag)
tvPassword.rx.text.map { $0 ?? "" }.bind(to: viewModel.passwordPublishSubject).disposed(by: disposeBag)
tvRepeatPassword.rx.text.map { $0 ?? "" }.bind(to: viewModel.repeatPasswordPublishSubject).disposed(by: disposeBag)
viewModel.formLoginNativaIsValid().bind(to: btnNext.rx.isEnabled).disposed(by: disposeBag)
checkBoxOutlet.rx.tap
.scan(false) { lastValue, _ in
return !lastValue
}
.bind(to: btnNext.rx.isEnabled)
.disposed(by: disposeBag)
}
Mon ViewModel :
let userNamePublishSubject = PublishSubject<String>()
let emailPublishSubject = PublishSubject<String>()
let passwordPublishSubject = PublishSubject<String>()
let repeatPasswordPublishSubject = PublishSubject<String>()
func formLoginNativaIsValid() -> Observable<Bool> {
return Observable.combineLatest(userNamePublishSubject.asObserver(),
emailPublishSubject.asObserver(),
passwordPublishSubject.asObserver(),
repeatPasswordPublishSubject.asObserver()).map { userName, email, password, repeatPassword in
return self.validateRegister(userName: userName, email: email, password: password,
repeatPassword: repeatPassword)
}.startWith(false)
}
private func validateRegister(userName: String, email: String, password: String, repeatPassword: String) -> Bool {
return userName.count >= 5 && email.isEmail() && password.count >= 5 && repeatPassword.count >= 5 && password == repeatPassword
}
J'ai validé le formulaire, mais j'ai également besoin que l'utilisateur accepte les conditions générales pour que le bouton "Suivant" soit activé.