Tooltips on form fields

Ext Class: 
Any Ext Form field
By default you cannot have tooltips on form fields using Ext Js.
This is because by default tooltips are used to display validation messages.
Tooltip validationTooltip validation
To override this and use tooltips to display user help you first need to turn off this default behaviour as follows:
    Ext.form.Field.prototype.msgTarget = 'side';
This sets the validation message target to beside the field.
Side validation messagesSide validation messages
Next you need to override the default onRender action for form fields as follows
Ext.override(Ext.form.Field, {
    onRender : function(ct, position){
        Ext.form.Field.superclass.onRender.call(this, ct, position);
        if(!this.el){
            var cfg = this.getAutoCreate();
            if(!cfg.name){
                cfg.name = this.name || this.id;
            }
            if(this.inputType){
                cfg.type = this.inputType;
            }
            if(this.tooltip){
                cfg['ext:qtip'] = this.tooltip.tip;
                cfg['ext:qwidth'] = this.tooltip.width || 100;
            }
            this.el = ct.createChild(cfg, position);
        }
        var type = this.el.dom.type;
        if(type){
            if(type == 'password'){
                type = 'text';
            }
            this.el.addClass('x-form-'+type);
        }
        if(this.readOnly){
            this.el.dom.readOnly = true;
        }
        if(this.tabIndex !== undefined){
            this.el.dom.setAttribute('tabIndex', this.tabIndex);
        }
        
        this.el.addClass([this.fieldClass, this.cls]);
        this.initValue();
    }
});
The key section here is
if(this.tooltip){
    cfg['ext:qtip'] = this.tooltip.tip;
    cfg['ext:qwidth'] = this.tooltip.width || 100;
}
The rest of the onRender function remains unchanged.
What this code does is allows for the definition of a tooltip config object with a tip and optional width as follows
new Ext.form.TextField({
    allowBlank: false,
    blankText: 'You must enter a value for customer',
    emptyText: 'Enter customer',
    maxLength: 100,
    selectOnFocus: true,
    name: 'customer',
    fieldLabel: 'Customer',
    maxLengthText: 'Customer cannot exceed 100 characters',
    tooltip:{
        tip:'Enter the customer\'s name',
        width: 150
    }
})
Which will render a standard tooltip as follows
Field with tooltipField with tooltip
Note you must enable quicktips in your document ready function as follows
Ext.QuickTips.init();
That was very helpful, straightforward and brief. Thank you so much!!!

Hi,

Thanks a lot for your snippet,

After porting it to extjs 3.2.1.

I realized that we could override getAutoCreate instead of onRender:

I also wrote a test suite to validate the correctness of its behaviour:

You can read about it at the end of my blog post

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
four plus equals fourteen
Solve this math question and enter the solution with digits. E.g. for "two plus four = ?" enter "6".