Meteor AutoForm-是否可以手动调用onSuccess或onError挂钩?

问题描述 投票:3回答:1

我有此表单可以检查用户的CAPTCHA输入。当验证码有效时,它应该发送一封电子邮件,否则应该显示错误。

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()钩子。

当用户提交错误的验证码时也是如此。它显示我的错误消息,但不触发onError()挂钩。

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

javascript meteor dom-events
1个回答
2
投票

根据AutoForm hooks documentation,在成功或失败的操作上调用onSuccessonError,不包括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挂钩中同时处理成功和错误情况。

© www.soinside.com 2019 - 2024. All rights reserved.