ExtJS 4 - Mark a red asterisk on an required field

后端 未结 11 2247
长发绾君心
长发绾君心 2020-12-08 08:24

I have this problem where I need to add a red asterisk beside a fieldLabel when a field is marked as \"required\" (or allowBlank: false)

In

11条回答
  •  执笔经年
    2020-12-08 08:32

    I've made a plugin for this.

    Works this ExtJS 4.1 (at least).

    Use if like this :

    Ext.create('Ext.form.Panel', {
      ...
      plugins : "formlabelrequired"
      ...
    });
    

    Or, to customize the "asterisk":

    Ext.create('Ext.form.Panel', {
      ...
      plugins : [{ptype:"formlabelrequired", asterisk:" (mandatory)"}]
      ...
    });
    

    Here is the code for the plugin :

    /**
     * Plugin (ptype = 'formlabelrequired') that adds "asterisk" to labels
     * for Fields with "allowBlank: false".
     */
    Ext.define('Ext.ux.plugin.form.LabelRequired', {
    
            extend: 'Ext.AbstractPlugin',
    
            alias: 'plugin.formlabelrequired',
    
            asterisk: '  *',
    
            constructor: function() {
    
                this.callParent(arguments);
    
            },
    
            init: function(formPanel) {
                formPanel.on('beforerender', this.onBeforeRender, this);
            },
    
            /**
             * @private
             * Adds asterisk to labels.
             */
            onBeforeRender: function(formPanel) {
    
                var i, len, items;
    
                items = formPanel.query('[allowBlank=false]');
    
                for (i = 0, len = items.length; i < len; i++) {
                    item = items[i];
                    item.afterLabelTextTpl = (item.afterLabelTextTpl || "") + this.asterisk;
                }
    
                return true;
    
            }
    
        });
    

提交回复
热议问题