【问题标题】:Ext JS 4.1 MessageBox with text validation带有文本验证的 Ext JS 4.1 消息框
【发布时间】:2012-07-04 13:44:51
【问题描述】:

我创建了一个允许用户输入文本的 MessageBox:

Ext.MessageBox.show({
    title : 'Reason',
    msg : 'Your reson:',
    width : 300,
    buttons : Ext.MessageBox.OKCANCEL,
    multiline : true,
    scope : this,
    fn : function(btn, reason){ if (btn == 'ok' && reason != '') this.rejectPlan(rec, reason);}
});

用户看到并允许输入他的原因,但现在我能做的就是验证他输入的文本是否为空。

我想阻止 OK 按钮,直到用户输入一些文本(假设至少 20 个字符)

我可以向 MessageBox 添加验证器还是必须创建自定义组件扩展窗口?

【问题讨论】:

    标签: extjs4 extjs4.1


    【解决方案1】:

    其实是有可能的,我认为可能需要一些努力才能让它发挥作用。这是一种相当“hacky”的方法,所以请在你自己的题外话中使用它。

        var messageBox = Ext.MessageBox.show({
                title: 'Type Something in!',
                msg: 'Please type something into the text box!',
                width: 300,
                buttons: Ext.MessageBox.OKCANCEL,
                multiline: true,
                fn: function(btn, text, config) {
                    //Do your thing
                },
        });
    
        messageBox.msgButtons.ok.setDisabled(true);
        messageBox.textArea.allowBlank = false;
        messageBox.textArea.on('change', function (e, text, o) {
            if (text.length > 0) {
                messageBox.msgButtons.ok.setDisabled(false);
            } else {
                messageBox.msgButtons.ok.setDisabled(true);
            }
        });
    

    显示后,您可以获得对消息框的引用。创建并显示文本框后,确定按钮被禁用,文本区域设置为不允许空白值。我还附加了一个事件处理程序,用于检查文本区域中是否有文本,以决定是否启用或禁用 ok 按钮。

    这种“变通方法”很容易受到 API 更改的影响,所以要小心。

    【讨论】:

    • 我已经有一段时间没有做过 ExtjS 了,但我肯定会试试这个。感谢您挖掘出这个老问题:)
    【解决方案2】:

    你可以在你的fn中添加opts参数,代表消息框的配置,如果文本为空,则重新打开一个消息框,配置相同。

    Ext.MessageBox.show({
        title : 'Reason',
        msg : 'Your reason:',
        width : 300,
        buttons : Ext.MessageBox.OKCANCEL,
        multiline : true,
        scope : this,
        fn : function(btn, reason, cfg){ 
             if (btn == 'ok' && Ext.isEmpty(reason)) { 
                //if you want to mark the text as mandatory
    
                Ext.MessageBox.show(Ext.apply({}, {msg:cfg.msg}, cfg));  
             } else if (btn =='ok') {
                alert('ok with text');                 
             }
        }
    });
    ​
    

    【讨论】:

    • 这是一个选项 :) 但是我可以在开始时禁用按钮,然后在输入测试后启用它吗?我认为使用 MessageBox 这将是有问题的。
    • 是的,如果我必须这样做,我会创建自己的窗口组件,看起来更容易。 MessageBox 不允许有很多变体,尤其是 Ext.MessageBox.show,它对所需的配置更加严格。
    【解决方案3】:

    您还可以像这样启用字段验证:

      var promptWindow = Ext.Msg.prompt( 'Reason', 'Please enter a reason of reverse:', function( btn, text, cfg ) {
            if( btn === 'ok' && text.length > 128){
                //display warning
    
                cfg.value = text;
                Ext.MessageBox.prompt( Ext.apply( {}, { msg: cfg.msg }, cfg ) );
            }
            else if ( btn === 'ok' ) {
                //do something on success
            }
        }, this, true );
        promptWindow.textArea.maxLength = 128; //if multiline is enabled
    //promptWindow.textField.maxLength = 128; //if multiline is disabled
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多