我是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....
当我按下按钮并检查所有验证时,显示警告......
但我想要做的就是如果一个验证失败那么它应该不会更进一步,直到先前的验证成功...
我不知道如何实现这一目标。任何帮助将不胜感激。
答案 0 :(得分:3)
你可以这样做。
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)
答案 1 :(得分: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
}
答案 2 :(得分: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)
}
}