2014-06-26 37 views
3

我有这种检查用户的CAPTCHA输入的表单。当验证码有效时,应发送电子邮件,否则应显示错误。Meteor AutoForm - 是否可以手动调用onSuccess或onError挂钩?

AutoForm.addHooks(["form1", "form2", "form3"], { 

    onSubmit: function(doc) { 

     Meteor.call('validateCaptcha', formData, function(error, result) { 

      if (result.success == true) { 
       Meteor.call('sendEmail', doc); 
      } else { 
       Recaptcha.reload(); 
       // call onError here 
       console.log("Error!"); 
      } 

     }); 
    }, 

    onSuccess: function(operation, result, template) { 
     // display success, reset form status 
     console.log("CAPTCHA Validation Success! Email Sent!"); 
    }, 

    onError: function(operation, error, template) { 
     // display error, reset form status 
     console.log("Error: CAPTCHA Validation failed!"); 
    } 
} 

现在我的代码的问题是,当用户提交一个正确的验证码,代码将发送一封邮件,但不会触发onSuccess()钩。

用户提交错误的CAPTCHA时也是如此。它显示我的错误消息,但不会触发onError()钩子。

有没有办法手动调用这些钩子?

回答

2

按照AutoForm hooks documentationonSuccessonError被称为成功的或失败的操作,不包括onSubmit

// Called when any operation succeeds, where operation will be 
// "insert", "update", "remove", or the method name. 
onSuccess: function(operation, result, template) {}, 

// Called when any operation fails, where operation will be 
// "validation", "insert", "update", "remove", or the method name. 
onError: function(operation, error, template) {}, 

如果你需要处理的成功/失败的案例为onSubmit,你应该在你if (result.success === true) ... else这样做直接编码。如果您想要更精细的控制,您也可以在每个操作的基础上处理after挂钩中的成功和错误案例。

相关问题