2016-06-18 49 views
0

如何在RAC命令或按鈕單擊後驗證表單?我有這樣的地方在我的視圖模型活性可可驗證

self.executeLoginRacCommand = RACCommand(enabled: activateButtonSignal, signalBlock: { (any : AnyObject!) -> RACSignal! in 

      return self.executeLoginAPI() 
     }) 

//My question is here, how can I display an alert to my view controller saying may email is invalid. 
//MARK: - Execute Login API 
    private func executeLoginAPI() -> RACSignal { 
     if self.isEmailValid(self.email) { 
      return self.signInServices.signInAPIService.signInAPISignal(self.email, password: self.password) 
     } else { 
      self.errorMessage = "Please insert a valid email" 
      return RACSignal.empty() 
     } 
    } 

//MARK: - Is Email Valid 
    func isEmailValid(email: String) -> Bool { 
     let emailRegEx = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-][email protected][a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$" 

     let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx) 
     return emailTest.evaluateWithObject(email) 
    } 

我訪問它在我的視圖控制器是這樣的:

self.signInButton.rac_command = self.signInViewModel.executeLoginRacCommand 

能否請您提出一個按鈕後,確認無效的電子郵件和顯示警報的好方法點擊?提前致謝!

+0

您是否爲自己運行「驗證電子郵件」搜索? –

+0

是的,我已經GOOGLE了很多,找不到答案。我試圖改變我的代碼。不幸的是它不起作用。 –

回答

0

在您的else分支中,您將返回一個空的RACSignal。 而是返回發送錯誤的RACSignal(RACSignal.error(<the error>))。

在您的ViewController中,您可以訂閱errors屬性self.executeLoginRacCommand並向用戶顯示錯誤。