简体   繁体   English

检查是否至少填写了一个表单字段

[英]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; 由于我有多种形式需要至少填写一个字段,因此我想在Util文件类中创建一个方法,并在控制器中调用该方法。 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 我会说,您的util方法应返回一个布尔值,例如

//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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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