4

我创建了一个 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 添加验证器还是必须创建自定义组件扩展窗口?

4

3 回答 3

5

实际上这是可能的,认为可能需要一些努力才能使其发挥作用。这是一个相当“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 更改的影响,所以要小心。

于 2015-03-24T19:58:06.853 回答
3

您可以将 opts 参数添加到您的 fn,它表示消息框的配置,如果文本为空,则重新打开一个消息框,具有相同的配置。

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');                 
         }
    }
});
​
于 2012-07-04T14:34:19.117 回答
0

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

  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
于 2019-06-27T07:08:34.383 回答