ok, it may be a little complicated, cause i created my own FormPanel extending the Ext.form.FormPanel. i shortened the code as much as possible.
this is the code for my form
Code:
eu.tv1.form.FormFromValues = function(config){
this.submitUrl = config.submitUrl;
this.bodyCfg = {
tag: 'form',
cls: this.baseCls + 'x-panel-body',
method : this.method || 'POST',
id : this.formId || Ext.id(),
action:this.submitUrl,
url:this.submitUrl
};
Ext.applyIf(config, {
title:"Form",
renderTo:config.target,
width: config.width,
listeners:{
'render':function(f){
f.body.set({action: f.submitUrl});
}
}
});
if(config.submitFunction){
this.submitFunction = config.submitFunction;
}
eu.tv1.form.FormFromValues.superclass.constructor.call(this, config);
}
Ext.extend(eu.tv1.form.FormFromValues, Ext.form.FormPanel,{
fields : [],
submitFunction:null,
submitUrl:"",
ajax:true,
addField:function(config){
var type = config.type;
if(type == "string"){
var field = new Ext.form.TextField({
id: config.id,
name: config.name,
validator: config.validator,
value: config.initialValue,
fieldLabel: config.title,
hideLabel: config.hideTitle,
inputType: config.inputType ? config.inputType : "text",
itemCls: config.itemClass,
style: config.style,
labelStyle: config.labelStyle,
width: config.width,
disabled: config.disabled
});
this.add(field);
this.fields.push(field);
} //.. other types follow
this.doLayout();
},
getValues:function(){
var result = {};
Ext.each(this.fields, function(field){
var name = field.getName();
if(field.getGroupValue){
result[name] = field.getGroupValue();
}
else if(field.getValueInMillis){
var val = field.getValue();
//window.console.log(val.format('j'));
result[name] = field.getValueInMillis();
}
else{
result[name] = field.getValue();
}
}, this);
return result;
},
addSubmitButton:function(config){
var btn = new Ext.Button({text:config.title, id:config.id});
btn.on("click", this.doOnSubmit, this);
this.add(btn);
this.doLayout();
},
doOnSubmit:function(e){
if(this.submitFunction != null){
this.submitFunction(this.getValues());
return;
}
if(this.ajax){
this.submitByAjax();
}
else{
this.body.dom.submit();
}
},
submitByAjax:function(){
Ext.Ajax.request({
url:this.submitUrl,
params:this.getValues(),
mehtod:"POST"
});
}
});
here is my createUser function
Code:
function createUser() {
var url = App.sema.userAdminUrl+'_json_user_create';
var createUserForm = new eu.tv1.form.FormFromValues({
submitUrl:url,
ajax:true,
width: 500,
submitFunction: submitCreateUserFormAndCloseWindow,
id: 'createUserForm',
title: ''
});
createUserForm.addField({
type:eu.tv1.form.Types.STRING,
title:"Vorname",
name:"name",
id: 'createUserName',
initialValue: "",
width: 250
});
createUserForm.addField({
type:eu.tv1.form.Types.STRING,
title:"Nachname",
name:"lastname",
id: 'createUserLastName',
initialValue: "",
width: 250
});
createUserForm.addField({
type:eu.tv1.form.Types.STRING,
title:"E-Mail",
name:"email_0",
id: 'createUserEmail_0",
initialValue: "",
width: 250
});
createUserForm.addField({
type:eu.tv1.form.Types.STRING,
name:"login",
title:"Login",
id: 'createUserLogin",
initialValue: "",
width: 250
});
createUserForm.addSubmitButton({
title:"Speichern"
});
var createUserWin = new Ext.Window({
collapsible:false,
modal:true,
title: "Neuen User anlegen",
layout: 'form',
id: 'createUserWindow',
items: createUserForm
});
createUserWin.show();
}
and this is the edit user function, where i have the iteration over the email addresses
Code:
function editUser(userId) {
var conn = new Ext.data.Connection();
conn.request({
scope:this,
url: App.sema.userAdminUrl + '_json_user_infos?id='+userId
});
conn.on('requestcomplete', function(conn, response, options) {
var responseData = Ext.util.JSON.decode(response.responseText);
var url = App.sema.userAdminUrl+'_json_user_edit';
var editUserForm = new eu.tv1.form.FormFromValues({
submitUrl:url,
ajax:true,
width: 500,
submitFunction: submitEditUserFormAndCloseWindow,
id: 'editUserForm',
title: ''
});
editUserForm.addField({
type:eu.tv1.form.Types.STRING,
title:"Vorname",
name:"name",
id: 'editUserName',
initialValue: responseData.name,
width: 250
});
editUserForm.addField({
type:eu.tv1.form.Types.STRING,
title:"Nachname",
name:"lastname",
id: 'editUserLastName',
initialValue: responseData.lastname,
width: 250
});
var emailList = responseData.emailList;
if (Ext.isArray(emailList) && emailList.length > 0) {
for(var i = 0; i < emailList.length; i++) {
var email = emailList[i];
editUserForm.addField({
type:eu.tv1.form.Types.STRING,
title:"E-Mail",
name:"email_"+i,
id: 'editUserEmail_"+i,
initialValue: email,
width: 250
});
}
}
editUserForm.addField({
type:eu.tv1.form.Types.STRING,
name:"login",
title:"Login",
id: 'editUserLogin",
initialValue: responseData.login,
width: 250
});
editUserForm.addSubmitButton({
title:"Speichern"
});
var editUserWin = new Ext.Window({
collapsible:false,
modal:true,
title: "User bearbeiten",
layout: 'form',
id: 'editUserWindow',
items: editUserForm
});
editUserWin.show();
});
}
and last but not least, this is the submit function i´m using (one of them, they only differ by the form)
Code:
function submitEditUserFormAndCloseWindow(valueMap) {
var form = Ext.getCmp('editUserForm');
Ext.Ajax.request({
url:form.submitUrl,
params:valueMap,
mehtod:"POST",
success: function(resp,opt) {
// ... do something
},
failure: function(resp,opt) {
// ... do something
}
});
}