Je me suis moqué d'un exemple de la façon dont je fais cela avec des comboboxes dans Ext JS 5.1 ... il est facilement portable vers le code Ext 4, il faut juste utiliser initComponent
au lieu de init
de ViewController.Voici le code (et Fiddle):
Ext.application({
name: 'Fiddle',
launch: function() {
Ext.define('MyComboViewController', {
extend: 'Ext.app.ViewController',
alias: 'controller.mycombo',
init: function() {
this.getView().setStore(this.createStore());
},
createStore: function() {
var store = Ext.create('Ext.data.Store', {
fields: [
{name: 'disp', type: 'string'},
{name: 'val', type: 'int'}
],
data: [
{disp: 'One', val: 1},
{disp: 'Two', val: 2},
{disp: 'Three', val: 3},
{disp: 'Four', val: 4},
{disp: 'Five', val: 5}
],
proxy: {
type: 'memory'
}
});
return store;
}
});
Ext.define('MyCombo', {
extend: 'Ext.form.field.ComboBox',
xtype: 'myCombo',
controller: 'mycombo',
displayField: 'disp',
valueField: 'val',
labelAlign: 'top',
validateOnChange: false,
typeAhead: true,
queryMode: 'local'
});
Ext.define('MyCombosContainerViewController', {
extend: 'Ext.app.ViewController',
alias: 'controller.mycomboscontainer',
init: function() {
var startCombo = this.lookupReference('startCombo');
var endCombo = this.lookupReference('endCombo');
startCombo.validator = Ext.bind(this.comboValidator, this, [startCombo, endCombo]);
endCombo.validator = Ext.bind(this.comboValidator, this, [startCombo, endCombo]);
},
comboValidator: function(startCombo, endCombo) {
return startCombo.getValue() < endCombo.getValue();
},
onSelectComboBox: function(combo) {
var startCombo = this.lookupReference('startCombo');
var endCombo = this.lookupReference('endCombo');
startCombo.validate();
endCombo.validate();
}
});
Ext.define('MyCombosContainer', {
extend: 'Ext.form.FieldContainer',
controller: 'mycomboscontainer',
layout: {
type: 'hbox',
align: 'stretch'
},
items: [{
xtype: 'myCombo',
reference: 'startCombo',
fieldLabel: 'Start',
listeners: {
select: 'onSelectComboBox'
}
}, {
xtype: 'myCombo',
reference: 'endCombo',
fieldLabel: 'End',
listeners: {
select: 'onSelectComboBox'
}
}]
});
Ext.create('MyCombosContainer', {
renderTo: Ext.getBody()
});
}
});
bonne réponse, mais comment effectuer la validation à un autre endroit, par exemple dans l'écouteur d'action d'un autre bouton. – fastcodejava
C'est aussi possible mais vous devrez obtenir des références à vos éléments via la portée ou 'Ext.getCmp()' si vous êtes dans un gestionnaire de boutons. Ensuite, effectuez la validation et marquez les champs comme étant invalides avec 'field.markInvalid ('...')' – ChrisR
En ce qui concerne @ChrisR, commentez l'obtention d'une référence. Les chances sont vos champs sont dans le même conteneur. Ainsi, vous pouvez facilement obtenir une référence à l'autre champ à l'intérieur de la fonction de validation. 'this' à l'intérieur de la fonction est le champ de texte sur lequel le validateur est configuré. Obtenir une référence à l'autre champ en utilisant: 'var otherField = this.up(). Down ('field [name = field_one]')' (ExtJS 4) –