【问题标题】:Extjs remote combo validation issueExtjs 远程组合验证问题
【发布时间】:2010-09-10 06:23:13
【问题描述】:

嗨, 我正在使用 extjs 3.2.1 lib,我是 extjs 的新手,

I have implemented combobox with remote with filtering option in the form, i have added forceSelection(true) property to combo for validate the text with store collection and allowblank(false) to combo for enable form save button .

我的问题是 forceSelection 属性仅验证模糊事件上的控件并清除组合文本,如果用户在组合中输入无效文本,则启用保存按钮(因为我只检查了组合的 allowblank(false) ) 在表单中,当他点击保存按钮时,它会在组合中提交无效文本。

我还检查了表单和组合的 isvalid() 方法,在保存事件中它也返回“真”。

如何在这种特定情况下进行验证?

【问题讨论】:

    标签: javascript extjs


    【解决方案1】:

    您可能会覆盖 ComboBox 中的 validateValue 函数,如果 forceSelection 设置为 true,请根据存储检查字段的原始值,如下所示:

    Ext.override(Ext.form.ComboBox, {
    
    validateValue : function(value) {
    
        var errs = this.getErrors(value);
    
        if((value || value != "") && this.forceSelection){
            var val = this.getRawValue(),
            rec = this.findRecord(this.displayField, val);
    
            if(!rec)
                errs.push("Invalid Selection"); 
        }
    
        var error = errs[0];
    
        if (error == undefined) {
            return true;
        } else {
            this.markInvalid(error);
            return false;
        }
    }
    

    });

    【讨论】:

      【解决方案2】:

      您可以为 blur 事件添加事件侦听器,以验证用户输入的文本。

      validateCombo = new function(field) {
          //business logic for your validation here.
          if(field.value == foo) { 
              //then do something
              field.isValid(true);  //the true boolean disables marking the field as invalid
          }
          else { 
             //do something else
             field.markInvalid('this field is invalid');
          }
      }
      
      var cb = new Ext.form.ComboBox({
      // all of your config options
      listeners:{
           scope: yourScope,
           'blur': validateCombo
      }
      

      });

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-23
        • 2011-11-25
        • 2012-03-30
        • 2011-07-31
        • 1970-01-01
        • 2011-11-13
        • 1970-01-01
        相关资源
        最近更新 更多