PDA

View Full Version : field validation to ensure that at least one of two fields is entered.



javabee
14 Sep 2010, 6:19 AM
Hi all, i have 2 fields on an extjs form. Whats the best way to use ExtJS validation to ensure that at least one (either) of the fields is populated?

Condor
14 Sep 2010, 6:21 AM
Have a look at the advanced vtypes example in the SDK (it has a validator for two datefields).

javabee
14 Sep 2010, 6:36 AM
mmm, I did that Condor but its different in the sense that it fires off the validation when both fields are populated or where one is then the user starts typing in the other... I guess i need my validation to regardless of keypress etc... truth is i'm not getting the initialFieldPass stuff... i'll dig a bit more in the docs.

javabee
14 Sep 2010, 7:10 AM
my Vtype hello world... can anyone see what i need to add/change to get something to fire :)

Ext.apply(Ext.form.VTypes,
{
contactNumberCheck : function(val, field) {
return false;
},
contactNumberCheckText : 'error text'
});

// defined form panel
{
xtype : 'textfield',
id : 'phoneMobile',
fieldLabel : 'Mobile Number',
vtype: 'contactNumberCheck'
allowBlank: false
}

javabee
14 Sep 2010, 1:53 PM
ok, different question. I'm gonna attach the test at the point when the form is submitted. Is there a way to 'fire' the validation formatting that puts the red underline and error message. I'm sure this isn't the best way to do it but it will suffice until a better solution could be understood.

Condor
15 Sep 2010, 12:13 AM
Example using validators instead of vtypes (maybe you understand this one better):

{
xtype: 'textfield',
fieldLabel: 'Field 1',
itemId: 'field1',
validator: function(value){
if (!value && !this.ownerCt.getComponent('field2').getValue()) {
return this.blankText;
}
return true;
}
},{
xtype: 'textfield',
fieldLabel: 'Field 2',
itemId: 'field2',
validator: function(value){
if (!value && !this.ownerCt.getComponent('field1').getValue()) {
return this.blankText;
}
return true;
}
}

javabee
15 Sep 2010, 1:01 AM
yep, thats done the job ... for whatever reason i dont know but the Vtype validation seemed to be ignored on the client validation check (that you also helped with:)..i.e
mainForm.on('clientvalidation',function(f, valid)
{ Ext.getCmp('submitForm').setDisabled(!valid); }

all other field validation disabled submit apart from 'custom' vtypes that were applied!?

Either way, this works great, thanks again.