开发者

Check all fields in a dijit form

开发者 https://www.devze.com 2023-04-10 00:08 出处:网络
Is there any way I can loop through all the dijit fields in a dijit form and print out whether they are valid. I only ask because my \'onValidStateChange\' function disables my submit button and I don

Is there any way I can loop through all the dijit fields in a dijit form and print out whether they are valid. I only ask because my 'onValidStateChange' function disables my submit button and I don't know why it wo开发者_JAVA百科nt re-enable it.


You get use the dijit.form.Form's getChildren() to get all of the child widgets in a form, and then use isValid() to check whether the field is valid;

var form, iterFunction;
form = dijit.byId('form');
iterFunction = function(widget){
    console.log(widget.isValid());
};
dojo.forEach(form.getChildren(), iterFunction);


By default, form.validate() will validate all the child widgets too. If any form widgets are added/removed dynamically to/from the form respectively, then connect all the child widgets and then validate

form = dijit.byId('form_id');
form.connectChildren();
form.validate();

if the form has multiple tabs (TabContainer), the "connectChildren" function will actually attach all the fields at all descendant levels to the parent form object. We need not run a loop manually, until unless the particular fields need to be known for any more manipulations.

For special case: If form contains many tabs and need to validate per form at a time and navigate for eg: cp1 and cp2 are the 2 tabs

var mytab1 = dijit.byId("cp1");
var canNavigate = true;
var highlight = function(widget){
   var v = widget.validate;
   if(!v) return true;
   widget._hasBeenBlurred = true;
   if(!widget.validate()) {
     canNavigate = false;
   }
};
dojo.forEach(mytab1.getChildren(), highlight);
if(canNavigate) {
   dijit.byId("tc").selectChild("cp2");
}

Complete example :- http://jsfiddle.net/ZtzTE/29/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号