2011-09-26 75 views
0

嗨我有一个问题与表单验证与dojo的表单dijit其中 我有两个或多个选项卡的形式。如果我有一个字段在选项卡 2上无效,但我在验证时(通过按提交/确定按钮)将其定位在tab1上,则验证方法会执行,但无法找到错误的dijit(切换到正确的选项卡)并专注于此。 invalidMessage出现,但位于窗口的左上角,而不是它应该在的位置的 。 有没有办法实现这个或解决方法适用于dojo 1.6?您可以提供的任何信息将不胜感激,谢谢。Dojo表单验证横跨标签

回答

1

如果你正在使用dijit.form.Form(它看起来像你),我认为最简单的方法是覆盖窗体的validate函数。看看在dijit.form.Form原来的验证函数的定义(在_FormMixin.js):

validate: function(){ 
      // summary: 
      //  returns if the form is valid - same as isValid - but 
      //  provides a few additional (ui-specific) features. 
      //  1 - it will highlight any sub-widgets that are not 
      //   valid 
      //  2 - it will call focus() on the first invalid 
      //   sub-widget 
      var didFocus = false; 
      return dojo.every(dojo.map(this.getDescendants(), function(widget){ 
       // Need to set this so that "required" widgets get their 
       // state set. 
       widget._hasBeenBlurred = true; 
       var valid = widget.disabled || !widget.validate || widget.validate(); 
       if(!valid && !didFocus){ 
        // Set focus of the first non-valid widget 
        dojo.window.scrollIntoView(widget.containerNode || widget.domNode); 
        widget.focus(); 
        didFocus = true; 
       } 
       return valid; 
      }), function(item){ return item; }); 
     }, 

你可以围绕if(!valid && !didFocus){条件改为查找验证失败的部件是哪个选项卡,并切换到它。

0

这适用于我。它只是检查父母是否是一个标签(有点丑陋,所以建议是受欢迎的),如果是,获取其父母并激活标签。 它扩展了scrollIntoView,因为这对我来说看起来是正确的功能。这意味着如果您在某个选项卡上将小部件滚动到视图中,该选项卡会自动激活。

只要确保此代码在某处运行。

define([ 
"dojo/_base/window", 
"dojo/aspect", 
"dojo/dom", 
"dojo/dom-class", 
"dojo/window", 
"dijit/registry" 
], function (baseWindow, aspect, dom, domClass, win, registry) { 
    //extend scrollintoview to make sure, if the element resides on a tab, the tab is activated first. 
    aspect.around(win, "scrollIntoView", function (originalScrollIntoView) { 
     return function (node, pos) { 
      node = dom.byId(node); 
      var doc = node.ownerDocument || baseWindow.doc, 
       body = baseWindow.body(doc), 
       html = doc.documentElement || body.parentNode; 
      if (node == body || node == html) { return; } 

      var widget = registry.byNode(node.domNode || node); 
      if (typeof widget != "undefined" && widget != null) { 
       if (typeof widget.getParent != "undefined") { 
        var par = widget.getParent(); 
        if (domClass.contains(node.domNode || node, 'dijitTabPane')) { 
         if (typeof par.selectChild != "undefined") { 
          try { 
           par.selectChild(widget); 
          } catch (e) { } 
          return; 
         } 
        } 
        if (domClass.contains(par.domNode, 'dijitTabPane')) { 
         this.scrollIntoView(par.domNode); 
        } 
       } 
      } 
     } 
     return originalScrollIntoView(node, pos); 
    }); 
});