PDA

View Full Version : Form submission when pressing enter



mrtwism
27 Sep 2007, 8:19 AM
I don't know if I'm missing something obvious here but I looked through the documentation and searched the forums and haven't really found an easy way to submit an ext generated form when the user hits enter while inputting data to one of the text fields. I'm not using the 'url' config parameter on the Form generation but have a submit button in the form that performs the action i want. Here's the code I'm using currently:


var loginForm = new Ext.form.Form({
labelWidth: 75
});

var usernameField = new Ext.form.TextField({
fieldLabel: 'User Name',
name: 'username',
width:175,
allowBlank:false
});

var passwordField = new Ext.form.TextField({
fieldLabel: 'Password',
name: 'password',
inputType: 'password',
width:175,
allowBlank:false
});

loginForm.add(usernameField, passwordField);

var submitButton = loginForm.addButton('Submit');

submitButton.on('click', function() {
this.disable();
this.setText("Sending info...");

var values = loginForm.getValues();

var authKey = getParam('authKey');
var apiKey = getParam('apiKey');
var username = values['username'];
var password = values['password'];

apiClientAction.grantPrivileges(apiKey, authKey, username, password, {
callback:function(str) {
if(str) {
Ext.MessageBox.alert('Success', 'Permissions granted.');
} else {
Ext.MessageBox.alert('Failure', 'Permission denied.');
}
submitButton.enable();
submitButton.setText("Submit");
},
errorHandler:function(message, exception) {
Ext.MessageBox.alert('Error', exception.message);
submitButton.enable();
submitButton.setText("Submit");
}
});
});

loginForm.render('form-ct');

usernameField.focus();

fay
27 Sep 2007, 9:28 AM
Take a look at KeyMap (http://extjs.com/deploy/ext/docs/output/Ext.KeyMap.html). I've used the same idea in a dialog:


var map = new Ext.KeyMap("login-dlg", {key: 13, fn: this.doSubmit, scope: dlgLogin});

mrtwism
27 Sep 2007, 10:01 AM
thanks a lot. that did the trick :)