【问题标题】:Is possible to do a custom binder on an array of input fields using RxSwift?是否可以使用 RxSwift 对输入字段数组进行自定义绑定?
【发布时间】:2020-01-11 03:19:54
【问题描述】:

我有两个输入字段,一个用于密码,另一个用于确认密码。我想创建一个自定义活页夹,它允许我比较两个输入字段的值,但也验证最小数量的字符。我有一个非常相似的问题,但不是关于比较两个不同的字段 (Is there some sort of Priority operator on RxSwift?),并且基于上一个问题的答案,我一直在尝试做这样的事情:

enum PasswordCreateValidation {
    case valid
    case lessThanMinimum
    case confirmationLessThanMinimum
    case differentInputs
}

extension Reactive where Base: [InputField] {
    var rxPassword: Binder<PasswordCreateValidation> {
        return Binder<[InputField]>(self.base) { control, value in
            switch value {
            case .valid :
                control[0].errorLabel.isHidden = true
                control[1].errorLabel.isHidden = true
                control[0].separator.backgroundColor = .white
                control[1].separator.backgroundColor = .white
            case .lessThanMinimum:
                control[0].errorLabel.isHidden = false
                control[0].separator.backgroundColor = .red
                control[0].errorLabel.text = "Needs more chars"
            case .confirmationLessThanMinimum:
                control[1].errorLabel.isHidden = false
                control[1].separator.backgroundColor = .red
                control[1].errorLabel.text = "Needs more chars"
            case .differentInputs:
                control[0].errorLabel.isHidden = false
                control[0].separator.backgroundColor = .red
                control[0].errorLabel.text = "Inputs are not the same"
                control[1].errorLabel.isHidden = false
                control[1].separator.backgroundColor = .red
                control[1].errorLabel.text = "Inputs are not the same"
            }
        }
    }
}

...

private func bind() {
    let codeMinimum = codeInputField.textField.rx.text.orEmpty.map { $0.count >= 1 }.skip(2)
    codeMinimum.bind(to: codeInputField.rx.nonEmpty).disposed(by: bag)

    let minimumAmountPassword = 8
    pwdInputField.minimumAmountOfChars = minimumAmountPassword
    confirmPwdInputField.minimumAmountOfChars = minimumAmountPassword

    let pwdMinimum = pwdInputField.textField
        .rx.text.orEmpty.map { $0.count >= minimumAmountPassword }.skip(2)
    let confPwdMinimum = confirmPwdInputField.textField
        .rx.text.orEmpty.map { $0.count >= minimumAmountPassword }.skip(2)

    pwdMinimum.bind(to: pwdInputField.rx.minimumChars).disposed(by: bag)
    confPwdMinimum.bind(to: confirmPwdInputField.rx.minimumChars).disposed(by: bag)

    let pwdEqualA = pwdInputField.textField.rx.text.orEmpty
        .map { $0 == self.confirmPwdInputField.value }.skip(2)

    let pwdEqualB = confirmPwdInputField.textField.rx.text.orEmpty
        .map { $0 == self.pwdInputField.value }.skip(2)

    let equality = Observable.combineLatest(pwdEqualA, pwdEqualB) { $0 && $1 }
    let minimum = Observable.combineLatest(pwdMinimum, confPwdMinimum) { $0 && $1 }
    let pwdValidation = Observable.combineLatest(equality, minimum) { $0 && $1 }

    Observable.combineLatest(pwdValidation, codeMinimum) { $0 && $1 }
        .startWith(false)
        .bind(to: signInButton.rx.isActive)
        .disposed(by: bag)
}

看来我的自定义活页夹是错误的。不是允许有一个数组作为Base吗?

【问题讨论】:

    标签: ios swift cocoa rxjs rx-swift


    【解决方案1】:

    我会创建一个包含两个输入字段的视图并将该视图用作基础

    【讨论】:

      猜你喜欢
      • 2012-02-18
      • 2017-04-04
      • 2020-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多