简体   繁体   中英

Check if at least one form field has been filled

To check if at least one forem field has been filled out, among other solutions, I am considering the following solution:

 var form = Ext.ComponentQuery.query('#myform')[0];

 form.getForm().getFields().each(function(field) {
       var value = field.getRawValue();
       if(value !== ''){
            //submit form
        }else{
             //error message
        }
  });

Since I have several forms that require filling in at least one field, I wanted to create a method in a Util file class and call this method in the controller; something like:

//Class Util
testFields: function(form){
    form.getForm().getFields().each(function(field) {
        var value = field.getRawValue();
        if(value !== ''){
           ...
        }
    });
},

//controller 
if(MyApp.util.Util.testFields(form) !== ''){ //does not work
     //submit form
}else{
    //error message
}

Is a solution like this feasible, or is it preferable to get the value of each field in the controller without iterating and testing if they are empty?

I would say, that your util method should return a boolean like

//Class Util
testFields: function(form){
    var result = false;
    form.getForm().getFields().each(function(field) {
        if(field.getRawValue()){  // at least one field needs to be filled out
           result = true;
        }
    });
    return result;
},

Than your controller method should just test form like

//controller 
if(MyApp.util.Util.testFields(form)){
     form.submit();
}else{
    //error message
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM