Quote from: shamblett at Jan 27, 2009, 03:47 AM
Ok splittingred the above code is fine.
What’s the best way to return error strings back to the form?
I want to say things like ’no template supplied’, ’no doc id’s’ supplied’ etc. if parameter validation fails. I can sort of do this using the $this->modx->toJSON call, setting ’success’ to 0 and an appropriate message in ’message’. I get the message box OK with the message but the box label says ’Success!’ which I don’t want. Also when you OK the box it vanishes but the manager screen doesn’t refresh, I have to F5 it to get any focus back.
I’m probably missing something here that’s easier than this.
Take a look at the MODx processors for examples, but you’ll use the error class. You’ll need to load it, if you haven’t, though, in your connector/processor loading code. So, for a success message:
return $modx->error->success();
And an error:
return $modx->error->failure('Message here.');
failure and success also take a 2nd parameter, which can be either an array, string, or xPDOObject (ie, a db object). This returns in MODx.FormPanel as o.result.object (described below).
In the MODx.FormPanel class (assuming your forms have been extending this), it automatically handles the error/response class by firing an event, either ’success’ or ’failure’. By default, this will proceed if a success, and popup an error window (without proceeding in the form) if there’s a failure.
If you want to add customized error handling, you’ll add listeners to your JS class that extends MODx.FormPanel. Those events are, ’failure’ and ’success’, and come passed with one parameter, which is a object of parameters. An example is this:
/*
* the param 'o' contains 4 keys:
* - o.form: the FormPanel object
* - o.result: the result object, which has o.result.message and o.result.object
* - o.options: the options passed to the FormPanel.submit function
* - o.config: The config passed to the FormPanel
*/
// this listeners param would go in your class's config, the class that extends MODx.FormPanel
listeners: {
'success': {fn:function(o) {
var r = o.result;
// let's update the page title on success
Ext.get('header').update('<h2>'+r.object.pagetitle+'</h2>');
},scope:this}
,'failure': {fn:function(o) {
// you dont need this listener unless you want to override the default behavior
this.errorMsg = o.result.message; // like so, for some odd reason
return true; // returning false will prevent the popup error message
},scope:this}
}