2012-09-06 23 views
0

我定义了一个验证,它控制着id是唯一的还是不绑定按钮。它很好地与内置验证很好,但它不绑定我自己的验证Ext JS 4.1 - 用户定义的验证不绑定按钮

这是我已经试过:

视图 - FormPanel中:

Ext.define(appName + '.view.user.UserForm', { 
    extend: 'Ext.form.Panel', 
    requires: [appName + '.view.language.LanguageCombo'], 
    alias: 'widget.userform', 
    // title  : 'User Form', 
    iconCls: 'icon-form', 
    frame: true, 
    padding: '5 5 0 5', 
    border: true, 
    buttonAlign: 'right', 
    width: '100%', 
    // height : 200, 
    monitorValid: true, 
    bodyPadding: 10, 
    fieldDefaults: { 
     labelAlign: 'left', 
     labelWidth: 110, 
     anchor: '98%', 
     allowBlank: false, 
     selectOnFocus: true, 
     msgTarget: 'side' 
    }, 
    initComponent: function() { 
     var me = this; 


     this.title = bundle.getMsg('userform.title'); 


     this.items = [{ 
      xtype: 'numberfield', 
      minValue: 1, 
      fieldLabel: bundle.getMsg('userform.field.recordId'), 
      name: 'recordId', 
      itemId: 'recordId' 
     }, { 
     ]; 


     this.btnReset = Ext.create('Ext.ux.button.ResetButton', { 
      handler: function (btn) { 
       me.getForm().reset(); 
      } 
     }); 


     this.btnSubmit = Ext.create('Ext.ux.button.SaveButton', { 
      disabled: true, 
      formBind: true 
     }); 


     this.buttons = [me.btnReset, me.btnSubmit]; 


     this.callParent(arguments); 
    } 
}); 

控制器的方法:

var form = this.getUserForm(); 
if (field.getValue() && field.getValue() != '') { 
    Ext.Ajax.request({ 
     url: 'user/chkRecordIdUnique.ajax', 
     method: 'POST', 
     params: { 
      recordId: field.getValue() 
     }, 
     success: function (response, options) { 
      var res = Ext.decode(response.responseText); 
      if (!res.success) { 
       field.markInvalid(bundle.getMsg('record.taken')); 
       form.getForm().markInvalid(bundle.getMsg('record.taken')); 
      } 
     } 
    }); 
} 

回答

1

根据该文档,markInvalid实际上并不改变字段的有效性。它只是应用视觉样式,就好像该字段有错误一样。并没有isValid属性设置。所有有效性均通过立即致电isValid方法确定。

就目前而言,Ext JS表格本身不支持异步验证。它假定所有验证都在客户端完成,或者服务器将执行验证。如果您希望在启用保存按钮之前执行Ajax调用以确定表单是否有效,那么我会建议您使用成功回调来制作自己的验证方法。如果Ajax调用成功并且表单的其余部分有效,则手动启用保存按钮。然后在表单更改上,您可以再次禁用保存按钮。

附注:width应该是一个数字,而不是百分比。

+0

@Erick库感谢您的关心,但实际上这只是一个代码示例。所以我也必须检查其他领域。所以我不能手动启用或禁用表单绑定按钮,或者我应该为每个用户定义的验证执行此操作;这是没有意义的.. – talha06

+1

如果每个字段都可以独立验证,那么可以使用同步Ajax在'field.validator'函数中执行验证。 Ext JS不支持通过'Ext.Ajax.request',所以你必须手动执行它(应该很容易设置)。我实际上不得不在最近自己做,所以这是可能的。如果字段依赖于其他字段,那么您应该考虑删除“formBind”,并仅仅进行所有验证/启用。 – Eric

+0

好的,非常感谢埃里克,看来我应该像你一样自己实现它。但是,如果核心图书馆做到了,它会更好;当一个标记为无效的字段(所以表单变为无效)时,也会生成一个formBind:true的按钮。 – talha06