0

我是 RxSwift 和 RxCocoa 的新手,我正在学习它。

我想在按钮单击时验证所有文本字段,并根据我需要向用户显示警报消息的验证。

验证成功后,我需要在表中插入记录。

参考以下代码...

var result = txtFname.rx.text
    result.asObservable().subscribe(onNext: { text in
        if text!.isEmpty {
            self.showAlert(msg: "Plese enter first name.")
            self.txtFname.becomeFirstResponder()
        }
    }).disposed(by: disposeBag)

    result = txtLname.rx.text
    result.asObservable().subscribe(onNext: { text in
        if text!.isEmpty {
            self.showAlert(msg: "Please enter last name.")
            self.txtLname.becomeFirstResponder()
        }
    }).disposed(by: disposeBag)

    result = txtEmail.rx.text
    result.asObservable().subscribe(onNext: { text in

        if text!.isEmpty {
            self.showAlert(msg: "Please enter email id.")
            self.txtLname.becomeFirstResponder()
        }
    }).disposed(by: disposeBag)

   //need to check here if all fields are valid or not 
   //if all fields are valid then insert record....

当我按下一个按钮并同时检查所有验证并显示警报时...

但是我想这样做,如果一个验证失败,那么在之前的验证成功之前它不应该更进一步......

我不知道如何实现这一点。任何帮助将不胜感激。

4

3 回答 3

5

你可以做这样的事情。

  self.button.rx.tap.asObservable()
     .filter({ (_) -> Bool in
        guard !(self.txtFname.text ?? "").isEmpty else {
           self.showAlert(msg: "Please enter first name.")
           self.txtFname.becomeFirstResponder()
           return false
        }

        guard !(self.txtLname.text ?? "").isEmpty else {
           self.showAlert(msg: "Please enter last name.")
           self.txtLname.becomeFirstResponder()
           return false
        }

        guard !(self.txtEmail.text ?? "").isEmpty else {
           self.showAlert(msg: "Please enter email id.")
           self.txtEmail.becomeFirstResponder()
           return false
        }

        return true
     })
     .subscribe { _ in
        // do something when all the fields are valid
        self.showAlert(msg: "All fields are valid")
     }
     .disposed(by: disposeBag)
于 2018-03-29T10:41:24.067 回答
1

这不是完全相同的答案,但您可以实现这样的目标。

      class DataValidator {
            class func validName(name:String) -> Bool {
            if let regex =
            try? NSRegularExpression(pattern: "^\\w+( \\w+\\.?)*$", options:
            .CaseInsensitive) {
            return name.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) > 2 &&
            regex!.matchesInString(name, options: NSMatchingOptions.ReportProgress, range: NSMakeRange(0,
            name.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))).count > 0 }
            return false }

            class func validEmail(email:String) -> Bool{ if let regex =
            try? NSRegularExpression(pattern: "^\\S+@\\S+\\.\\S+$", options: .CaseInsensitive){
            return regex!.matchesInString(email, options: NSMatchingOptions.ReportProgress, range: NSMakeRange(0,
            email.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))).count > 0 }
        return false }
 }

}// end of validator class

您可以使用以下内容:

let nameSignal:RACSignal = nameTextField.rac_textSignal().map { (text) -> AnyObject! in
return DataValidator.validName(text as! String) }
let emailSignal = emailTextField.rac_textSignal().map { (text) -> AnyObject! in
return DataValidator.validEmail(text as! String) }

RACSignal.combineLatest([nameSignal, emailSignal]).subscribeNext { (valid) -> Void in
   self.button.enabled = valid as! Bool
}
于 2018-03-29T10:59:24.340 回答
0
class Validator {

    class func validEmail(email:String) -> Bool {
        if let regex = try? NSRegularExpression(pattern: "^\\S+@\\S+\\.\\S+$", options: .caseInsensitive){
        return regex.matches(in: email, options: NSRegularExpression.MatchingOptions.reportProgress, range: NSMakeRange(0,                                                                                                                        email.lengthOfBytes(using: String.Encoding.utf8))).count > 0 }
        return false
    }
}

然后你可以按如下方式使用它:

var isValid : Observable<Bool>{
    return Observable.combineLatest(username.asObservable())
    { (username) in
        return Validator.validEmail(email: username)
    }
}
于 2018-09-22T10:07:40.890 回答