将表单标记为无效

发布于 2025-01-05 15:41:02 字数 1304 浏览 3 评论 0原文

在 FormPanel 的字段中,我添加了监听器 blur。如果发生此事件,我将执行表单验证,如下所示:

listeners:{
    scope: this,
    blur: function(field, value){
        var username = field;
        if (username.getValue() == '') {
            username.markInvalid('This field is required');
        } else {
            Ext.Ajax.request({
                url: 'users/validateForm',
                method: 'POST',
                params: 'username=' + username.getValue(),
                success: function(response, opts) {
                    var jsonData = Ext.decode(response.responseText);
                    if (jsonData.msg.username != '') {
                        username.markInvalid(jsonData.msg.username);
                    }
                }
            });
        }
    }
}


buttons: [{
    text: 'Save',
    handler: function() {
        var form = this.up('form').getForm();
        if (form.isValid()) {
            alert('test');
        }
    },
    formBind:true
},{
    text: 'Cancel'
}]

正如 Ext JS 的文档所解释的, markInvalid() 函数仅影响字段本身。但我还想将表单设置为无效,以便在表单无效时禁用提交按钮。目前,尽管字段被标记为无效,但表单上的 isValid() 函数始终返回 true。

我现在的问题是,如何才能通过上面的代码将表单设置为无效?

非常感谢您的帮助!

On my field(s) in the FormPanel I added the listener blur. If this event occurs I execute the form validation as follows:

listeners:{
    scope: this,
    blur: function(field, value){
        var username = field;
        if (username.getValue() == '') {
            username.markInvalid('This field is required');
        } else {
            Ext.Ajax.request({
                url: 'users/validateForm',
                method: 'POST',
                params: 'username=' + username.getValue(),
                success: function(response, opts) {
                    var jsonData = Ext.decode(response.responseText);
                    if (jsonData.msg.username != '') {
                        username.markInvalid(jsonData.msg.username);
                    }
                }
            });
        }
    }
}


buttons: [{
    text: 'Save',
    handler: function() {
        var form = this.up('form').getForm();
        if (form.isValid()) {
            alert('test');
        }
    },
    formBind:true
},{
    text: 'Cancel'
}]

As the documentation of Ext JS explains, the markInvalid() function affects only the field itself. But I want also setting the form as invalid, so that the submit button is disabled when the form is invalid. Currently the function isValid() on the form always returns true, although the field(s) are marked as invalid.

My question now is, how can I achieve so that the form is also set as invalid with the code above?

Thank you very much for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小苏打饼 2025-01-12 15:41:02

您应该能够使用表单中提供的 hasInvalidField() 方法。例如:

var form = this.up('form').getForm();
if (form.hasInvalidField()) {
    alert('test');
}

来自文档:

如果表单包含任何无效字段,则返回 true。

You should be able to make use of the hasInvalidField() method available with the form. For example:

var form = this.up('form').getForm();
if (form.hasInvalidField()) {
    alert('test');
}

From the docs:

Returns true if the form contains any invalid fields.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文