PDA

View Full Version : Form posts and error handling using ext.direct PHP



Max_nl
29 Jun 2009, 5:30 PM
Hi,

I am new to ext.js and am experimenting with form submits in combination with the Ext.Direct PHP stack.
In another thread (http://extjs.com/forum/showthread.php?t=71339) it is suggested to use the api property of the form for that.

So I created a setup among the lines of:



Ext.Direct.addProvider(Ext.app.REMOTING_API);

var form = new Ext.form.FormPanel({
api: {
submit: MyClass.submit
},

[...other form stuff...]

buttons: [{
text: 'Submit',
handler:function(){
form.getForm().submit();
}
And on the server I created a simple class like:



class MyClass {
/**
* @remotable
* @formHandler
*/
function submit($fields, $files)
{
[ .... code that inserts the information from the $fields array to the database ....]
}
}

While this seems to work, it is not clear to me what my submit( ) function should do in case an error occurs on the server, or if something is wrong with the data the user entered.

If I simply throw a PHP exception, it results in a javascript error on the browser:



Error: result is undefined
Source File: http://[ip]/ext/ext-all-debug.js
Line: 35527
So apparently that is not the way to go.
Do I have to "return" something instead to indicate an error to the client?

elishnevsky
29 Jun 2009, 7:18 PM
This might help: http://extjs.com/deploy/ext-3.0-rc2/docs/?class=Ext.form.Action.Submit

Max_nl
30 Jun 2009, 8:32 AM
Thanks for the tip.
The following PHP code indeed causes a red border around the form field affected.


return array(
"success" => false,
"errors" => array("name-of-form-field" => "message")
); And after adding this to the Javascript, it also displays the message.


Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'side';Is there also a standardized way to alert the client of global errors that are not specific to a single form field?
E.g. when the form information could not be added to the database, because the database server is down.

Or do I need to attach a custom callback to the "failure" property, and handle that myself?

elishnevsky
30 Jun 2009, 8:41 AM
Is there also a standardized way to alert the client of global errors that are not specific to a single form field?
E.g. when the form information could not be added to the database, because the database server is down.

Or do I need to attach a custom callback to the "failure" property, and handle that myself?

It's a good question. I'm not sure. In Ext JS 3.0 there's a new Ext.Error class (http://extjs.com/deploy/ext-3.0-rc2/docs/?class=Ext.Error), you can try to use it. I'm going to look into it myself.