PDA

View Full Version : How can I get an error message after a form load?



dvarela
15 Sep 2008, 3:22 AM
I've changed the Loading/Saving a Dynamic Form using XML example (http://extjs.com/deploy/dev/examples/form/xml-form.html) as follows:


// A reusable error reader class for XML forms
Ext.form.XmlErrorReader = function(){
Ext.form.XmlErrorReader.superclass.constructor.call(this, {
record : 'field',
success: '@success'
}, [
'id', 'msg'
]
);
this.read = this.read.createSequence(function(){
Ext.MessageBox.alert('READED!','Executed after an Ext.form.XmlErrorReader.read()');
});
};
Ext.extend(Ext.form.XmlErrorReader, Ext.data.XmlReader);;

If the submit action gets an error

<?xml version="1.0" encoding="UTF-8"?>
<response success="false">
<errors>
<field>
<id>first</id>
<msg><![CDATA[
Invalid name. Blahblahblah.
]]></msg>
</field>
<field>
<id>dob</id>
<msg><![CDATA[
Invalid Date of Birth. Blahblahblah.
]]></msg>
</field>
</errors>
</response>
the message box alert is executed, but if the load action gets the same error no alert is shown. So if the read method isn't called afer a load error, how can I get an error message afer a load action?

Probably this thread (https://extjs.com/forum/showthread.php?p=127583) is about the same matter.

santosh.rajan
15 Sep 2008, 3:27 AM
What kind of error are you expecting after load?

dvarela
15 Sep 2008, 5:43 AM
What kind of error are you expecting after load?

Any error retreiving the data in the server (any database error, etc.) I'd like to make the same error treatment in both cases (after a load response and after a submit response).

Exploring the Ext code I found a solution to my problem but I don't know if it's the best:

Ext.override(Ext.form.Action.Load,{
handleResponse : function(response){
if(this.form.errorReader){
var rs = this.form.errorReader.read(response);
var errors = [];
if(rs.records){
for(var i = 0, len = rs.records.length; i < len; i++) {
var r = rs.records[i];
errors[i] = r.data;
}
}
if(errors.length < 1){
errors = null;
}
return {
success : rs.success,
errors : errors
};
}
return Ext.decode(response.responseText);
}
});

I'm using the same handleResponse method so I can do something like this:

Ext.onReady(function(){
var fs = new Ext.FormPanel({
...
// reusable eror reader class defined at the end of this file
errorReader: new Ext.form.XmlErrorReader()
)};


fs.on({
actionfailed: function(form, action){
if(action.type == 'submit'){
MyNameSpace.actionFailedMessage(form, action, "Submit error while ...");
}
if(action.type == 'load'){
MyNameSpace.actionFailedMessage(form, action, 'Load error while ...');
}
});
});

Ext.namespace('MyNameSpace');
MyNameSpace.actionFailedMessage = function(form, action, msg){
var _msg = '';
if(action.result){
_msg = (msg)? msg + '<br/><br/>' : '';
if(action.result.errors){
for(var i = 0, l = action.result.errors.length; i < l; i++){
_msg += ' ' + action.result.errors[i].msg + ' <br/>';
}
}
}else{
_msg += 'Default error text';
}
Ext.MessageBox.alert('ERROR',_msg);
};

dvarela
15 Sep 2008, 8:00 AM
I'm sorry but it doesn't work. I'm looking for a new solution. Any ideas?

santosh.rajan
15 Sep 2008, 8:17 AM
I think errorReader is only for submit. Return json instead for load. see docs for Action.load

mjlecomte
15 Sep 2008, 8:19 AM
How about returning success:false and then do as http://extjs.com/deploy/dev/docs/?class=Ext.form.Action.Load&member=failure indicates to extract the response from the server?

dvarela
15 Sep 2008, 11:46 PM
It does not work exactly as I want, but it seems the best solution. I will have to make an different error handling in each case.

Thx!

dvarela
16 Sep 2008, 2:56 AM
This is the code I'm looking for. I defined my default failure method. It's the same as the handleResponse method of Action.Submit:


Ext.override(Ext.form.BasicForm,{
load : function(options){
//default load options
var _options = {
failure: function(form, action){
if(form.errorReader){
var rs = form.errorReader.read(action.response);
var errors = [];
if(rs.records){
for(var i = 0, len = rs.records.length; i < len; i++) {
var r = rs.records[i];
errors[i] = r.data;
}
}
if(errors.length < 1){
errors = null;
}
action.result = {
success : rs.success,
errors : errors
};
}else{
action.result = Ext.decode(action.response.responseText);
}
}
}
//specific load options
Ext.apply(_options, options);

//do the load
this.doAction('load', _options);
return this;
}
});