PDA

View Full Version : Error handling in Form submit



piziwate
7 Oct 2007, 2:31 AM
Hello !

I'm working on an Ajax Form. The form post the data to a Php Script. The Php Script return an Json answer...


{"success":false,"errors":[{"id":"at0000000003","msg":"bad value"},{"id":"global","msg":"Internal server error"}]}

I want to implement errors based on form fields, that works fine... and I want to have a "global" error to inform the user of an server side error (database problem, ...).

How can I extract my global error from the action.result.errors object ?

Thank you for your help !

Regards !



this.addButton({
text: 'Save',
handler: function(){
fromAddCompany.form.submit({
url:'data.php',
waitMsg:'Saving...',
failure:
function(frm, act) {
alert(act.result.errors);
},
success:
function(frm, act) {
Ext.MessageBox.alert('Yes', 'Great!');
}
});
}
});

MuratCorlu
13 Jan 2008, 9:13 AM
Could you solve this problem? I'm trouble with same problem.

mmarti
4 Feb 2008, 3:06 AM
I'm also have the same problem, any solution you can post here?

vmorale4
13 Feb 2009, 9:28 AM
For a global error change your JSON response to have an empty errors array, and instead put the error message in a different property, i.e.:

Server Response:


{success:false, errors:[],msg:'Your session has expired'}


In this example I'm using a property called msg. Then in your failure handler read this property to display your alert



failure: function(form, action) {
switch (action.failureType) {
case Ext.form.Action.CLIENT_INVALID:
msg("Failure", "Form fields may not be submitted with invalid values");
break;
case Ext.form.Action.CONNECT_FAILURE:
msg("Failure", "Ajax communication failed");
break;
case Ext.form.Action.SERVER_INVALID:
if(action.result.errors.length>0) {
//your code for processing field errors
}
else {
msg("Failure", action.result.msg);
}
}
}

vmorale4
13 Feb 2009, 9:30 AM
Oops..I just realized this was posted in the Ext 1.x forum. My solution is intended for Ext 2.x