PDA

View Full Version : [CLOSED]Multiple Errors on display screen



ktoole
02-22-2010, 10:09 AM
How can I get multiple field errors to display on the screen? I tried to send back Json data with FLD and MSG in multiple times and found it ignored all but the last one. I figured out it needs to be in an array for ExtJS to interpret it correctly. How do I get the FLD and MSG into an array? I keep getting the incorrect format when I send it using:
vvOut_toJsonPair(%trim(ErrorMessages));
The Json looks like this:

{"SUCCESS":"0","ERRORS":"[{FLD:add_BNFARMNO","MSG":"Farm # may not be blank and must be a valid farm.","FLD":"add_BNBARNID","MSG":"The Barn ID may not be blank.}]"}

This is missing the quotes on the first FLD and it's data and also puts a quote on the [ and the other ].

Below is the ExtJS code to display the errors. Do I need some kind of loop for multiple fields in error or will ExtJS just sense the array and know how to handle it.


success: function(response){
var check = response.responseText;
if (check) {
var data = Ext.util.JSON.decode(response.responseText);
if (data.SUCCESS == '1') {
Ext.getCmp('addWindow').hide();
parent.showMessage('','<center><b>Record Added</b></center>');
dsMainMLPMABN2Grid.reload();
} else {
if (data.FLD==null){Ext.Msg.alert('Error',data.MSG);}
else{
f=Ext.getCmp(data.FLD);
f.markInvalid(data.MSG);
f.focus();
}
};
Ext.getBody().unmask();
};
},

sean.lanktree
02-24-2010, 01:04 AM
I would not recommend using vvOut_toJsonPair to send back this type of response. Use an external data structure array and load an element for each error and then use vvOut_toJson. For example, I will use the VVGENDS1 data structure, it contains two fields: VVREC and VVVALUE.



d errorArray e ds extname(vvgends1) qualified inz
d dim(10)
/free
// load all of the errors...
// I am hard coding the element number for simplicity
//
errorArray(1).vvrec = 'fieldA';
errorArray(1).vvvalue = 'Field A is in error';

errorArray(2).vvrec = 'fieldB';
errorArray(2).vvvalue = 'Field B is in error too';

vvOut.rootName = 'ERRORS';
vvOut.object = 'VVGENDS1';
vvOut.toJson(vvOut:%addr(errorArray):2);
/end-free



Now lets take a look at the front end. First, we need a data store...



myStore = new Ext.data.JsonStore({
root : 'ERRORS',
fields : ['VVREC','VVVALUE'],
listeners : {
load: function(){
this.each(function(rec){
Ext.getCmp(rec.data.VVREC).markInvalid(rec.data.VV VALUE);
})
}
}
})


Last part, loading the data store with the response from the back-end program...



myStore.loadData(Ext.util.JSON.decode(response.res ponseText));


After the store is loaded, it will trigger off the "load" listener which will loop through the records and mark each field in error invalid.

richard.milone
02-24-2010, 10:35 AM
Sean's example is excellent, but you could stick with a standard Ajax call on the front end if you want instead of using a JsonStore to hold the error results. In your existing Ajax request you could add this to the success area to loop through the errors that Sean's RPG program example would send back. This example assumes the root is ERRORS and the field names in each record are VVREC and VVVALUE. This loop example is valid to use anytime you need to process an array of response records from an Ajax request. Here I just have an alert coming up to show you that it is looping through each record but you can of course change it to apply errors to your fields however you need.



for (idx=0; idx<=data.ERRORS.length-1; idx++) {
alert(data.ERRORS[idx].VVREC + ' ' + data.ERRORS[idx].VVVALUE);
};

ktoole
02-25-2010, 09:21 AM
Thank you both for your suggestions. Based on your code I have it working just fine now. Thanks again.