将表单标记为无效
在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够使用表单中提供的
hasInvalidField()
方法。例如:来自文档:
You should be able to make use of the
hasInvalidField()
method available with the form. For example:From the docs: