Wednesday, April 18, 2007

Dojo's Formbind


dojo.require("dojo.io.*");

var x = new dojo.io.FormBind({
formNode: "configureDirectoryForm",
mimetype: 'text/json',
load: function(type, data, e) {
alert("type=" + type + "Data=" + data);
}
});

x.onSubmit = function(form) {
// validate form or dispaly loading msg
return true; // need this, otherwise form won't get sent!
}

Alternatively,

dojo.io.bind({
formNode: dojo.byId("agentCreationForm"),
method: 'post',
mimetype: 'text/json',
load: function(type, data, e) {
alert("type=" + type + "Data=" + data.state);
msgDialog.hide();
},
error: function(type, data, e) {
alert("An error occured!");
}
});

However, it has the limitation in form validation. It only allow client side JS validation. If we use webwork's validation framework, the response is html page with error msg rather than JSONObject string and cannot be interpreted.

1 comment:

bawb said...

I'm using a de facto Ajax differentiation standard to prevent broken error messages:

new dojo.io.FormBind({
...
'headers': {
'x_requested_with': 'XMLHttpRequest'
},
...
});

Then just test for that header on the server side and vary output accordingly.