PDA

View Full Version : Saving the login formpanel username as state



abraxxa
14 Jul 2010, 3:18 AM
I want to save the username as state of my login formpanel but can't figure out if/how it's possible to save the values of the form items in getState so they are properly restored and how to use the BasicForm's actioncomplete event for triggering the state save.

Condor
14 Jul 2010, 3:26 AM
Wouldn't it be a lot easier to use Ext.util.Cookies.get/set instead of trying to make the form stateful?

abraxxa
14 Jul 2010, 3:58 AM
I knew that asking here is the best way to get good code! ;-)

That's how my login form looks like now:


Ext.onReady(function(){
var loginForm = new Ext.form.FormPanel({
title:'Login',
standardSubmit:true,
url:'[% c.uri_for('/login') %]',
frame:true,
labelWidth:50,
width:350,
heigth:200,
defaults: {
xtype:'textfield',
allowBlank:false,
width:165
},
items:[{
id:'username',
blankText:'Enter your username',
fieldLabel:'Username'
},{
id:'password',
inputType:'password',
blankText:'Enter your password',
fieldLabel:'Password'
}
],
buttons: [{
text:'Login',
type:'submit',
plugins:'defaultButton',
handler:function(){
var form = loginForm.getForm();
if(form.isValid()){
Ext.util.Cookies.set('nac_web_nac_login_username', form.findField('username').getValue());
form.submit();
}
}
}],
listeners:{
'afterrender':function(){
var username = Ext.util.Cookies.get('nac_web_nac_login_username');
var form = this.getForm();
var username_field = form.findField('username')
if (! Ext.isEmpty(username)) {
username_field.setValue(username);
form.findField('password').focus();
}
else {
username_field.focus();
}
}
},
renderTo:'login'
});
});

beegeedee
14 Jul 2010, 3:58 AM
Try Ext.state.Manager.get("username"); and Ext.state.Manager.set("username",myusername);
depends on when you want to capture the username but you could do it on clicking the login button or after successful login, either works. Restoring it would be as easy as getting the username and setting it on creation of the control.