PDA

View Full Version : how to validate value in datefield



marc123
20 Sep 2012, 12:49 AM
Hi,

I have a Ext.grid.GridPanel containing a column "Valid from" and "Valid To". When editing a row or adding a new row (selection model=rowediting) I want the date in the "Valid To" field to be _after_ the date in the "Valid From" field. In the validator of the "Valid To" field I want to access the value entered in the "Valid From" field. How do I do this? Code:


application.grid.userMaintenance = Ext.extend(Ext.grid.GridPanel, {
[...]
initComponent:function() {
var config = {
[...]
columns: [
[...],
{
xtype: 'datecolumn',
name: 'startValidityDate',
header: 'Valid from',
dataIndex: 'START_VALIDITY_DATE',
sortable: true,
format: 'd-n-Y',
editor:{
xtype: 'datefield',
allowBlank: false,
altFormats:'d/n/Y|d n Y',
minValue: (new Date()).format('m/d/Y'),
minText: 'Can\'t set a start date in the past.'
//maxValue: (new Date()).format('m/d/Y')
}
},{
xtype: 'datecolumn',
header: 'Valid until',
dataIndex: 'END_VALIDITY_DATE',
sortable: true,
format: 'd-n-Y',
editor: {
xtype: 'datefield',
allowBlank: false,
altFormats:'d/n/Y|d n Y',
minValue: (new Date()).format('m/d/Y'),
minText: 'Can\'t set a end date in the past.',
//maxValue: (new Date()).format('m/d/Y')
validator: function(value){
var grid=Ext.getCmp('userGrid');
if(Ext.isDefined(grid)){
}else{
return true;
}
}
}
},
[...]

How can I compare the values in fields "Valid From" and "Valid To"?

Thanks,

Marc

marc123
20 Sep 2012, 6:00 AM
Hi Marc,

I'd add this for the 2 rowcolumns (note the value for the validate property):


,{
xtype: 'datecolumn',
name: 'startValidityDate',
header: 'Valid from',
dataIndex: 'START_VALIDITY_DATE',
sortable: true,
format: 'd-n-Y',
editor:{
xtype: 'datefield',
allowBlank: false,
altFormats:'d/n/Y|d n Y',
minValue: (new Date()).format('m/d/Y'),
minText: 'Can\'t set a start date in the past.',
//maxValue: (new Date()).format('m/d/Y'),
validationEvent: 'change',
validator: function(value){
console.log('validating '+value)
//store the START_VALIDITY_DATE to compare it with the END_VALIDITY_DATE
startValidityDate=value;
if(typeof endValidityDate !== 'undefined'){
if(startValidityDate < endValidityDate){
return true;
}
else{
return false;
}
}
else{
return true;
}
}
}
},{
xtype: 'datecolumn',
header: 'Valid until',
dataIndex: 'END_VALIDITY_DATE',
sortable: true,
format: 'd-n-Y',
editor: {
xtype: 'datefield',
allowBlank: false,
altFormats:'d/n/Y|d n Y',
minValue: (new Date()).format('m/d/Y'),
minText: 'Can\'t set a end date in the past.',
//maxValue: (new Date()).format('m/d/Y'),
validationEvent: 'change',
validator: function(value){
console.log('validating '+value)
//store the END_VALIDITY_DATE to compare it with the START_VALIDITY_DATE
endValidityDate=value;
if(typeof startValidityDate !== 'undefined'){
if(startValidityDate < endValidityDate){
return true;
}
else{
return false;
}
}
}
}
},

The value for validationEvent currently is 'change'. This means that when onChange fires the function is called. Default validationEvent is"keyup"

Thanks,

Marc