How can I refer to an object field inside same object during initialization? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How can I refer to an object field inside same object during initialization? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have a form. And lots of textfields in it. I use allowBlank property not to allow user to left the field blank and blankText to define custom “blank error” text.

{
 xtype: 'textfield',
 fieldLabel: 'Surname',
 name: 'person_surname',
 allowBlank: false,
 blankText: 'Please fill "Surname" field',                  
},

My question is:

Is there a way to use Ext.form.field.Text’s object property inside of another propery of the same object?

Something like:

{
 xtype: 'textfield',
 fieldLabel: 'Surname',
 name: 'person_surname',
 allowBlank: false,
 blankText: 'Please fill "' + fieldLabel + '" field',                  
},

(doesn’t work)

or:

{
 xtype: 'textfield',
 fieldLabel: 'Surname',
 name: 'person_surname',
 allowBlank: false,
 blankText: 'Please fill "' + this.fieldLabel + '" field',                  
},

(doesn’t work either)

Update:

For example in minLengthText property I can use template driven text like ‘The minimum length for this field is {0}’, which takes minLength property value of the same object.

I repeat: I use ExtJS’s object initialization method whith object literal syntax like:

{
 xtype: 'textfield',
 fieldLabel: 'Surname',
 name: 'person_surname',
 allowBlank: false,
 blankText: 'Please fill "Surname" field',                  
},

This syntax is very neat, using another syntax is not welcome here.

Answer

try adding listener to your form if you don’t want to rewrite your code

example

listeners : {

               render : function(){
                   Ext.each(form.items.items,function(fld,idx){
                    if(fld.getXType()=='textfield'){
                       fld.blankText = fld.fieldLabel;
                    }
                   });
               }
 }
We are here to answer your question about How can I refer to an object field inside same object during initialization? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji