gpt4 book ai didi

ios - 是否可以使用 RxSwift 在输入字段数组上执行自定义 Binder ?

转载 作者:行者123 更新时间:2023-11-28 23:20:56 25 4
gpt4 key购买 nike

我有两个输入字段,一个用于密码,另一个用于确认密码。我想创建一个自定义 Binder ,让我可以比较两个输入字段的值,但也可以验证最少的字符数。我有一个非常相似的问题,但不是关于比较两个不同的字段 ( 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)
}

似乎我的自定义 Binder 有误。不允许使用数组作为 Base 吗?

最佳答案

我会制作一个具有两个输入字段的 View ,并使用该 View 作为基础

关于ios - 是否可以使用 RxSwift 在输入字段数组上执行自定义 Binder ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59691355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com