PDA

View Full Version : Store save with server-side validation errors



NicoCipo
11 Dec 2009, 12:39 PM
I have an EditorGridPanel with a DirectStore. Update worked fine so far: json data is sent to server and response is parsed correctly if no validation errors occur in the server.

But, what if i want to validate data in the server and return any error message if it fails (like the behaviour supported in BasicForm)??. I need to mark the row or cell as invalid and also need to prevent the store commiting the invalid record.

Any suggestion will be appreciated.

Regards,
Nicolás

NicoCipo
15 Dec 2009, 4:27 AM
I have an EditorGridPanel with a DirectStore. Update worked fine so far: json data is sent to server and response is parsed correctly if no validation errors occur in the server.

But, what if i want to validate data in the server and return any error message if it fails (like the behaviour supported in BasicForm)??. I need to mark the row or cell as invalid and also need to prevent the store commiting the invalid record.

Any suggestion will be appreciated.

Regards,
Nicolás

Hello!!!! Is there someone who can at least give me a hint!?!?

valdemark
25 Jul 2010, 10:53 AM
I'm sorry to dig-up an old thread but I was wondering the same thing today. Couldn't find anything useful so far.

Is there any direct way to achieve this? Did you find an answer to your question Nicolas?

valdemark
25 Jul 2010, 1:05 PM
I had a look at firebug console output, searched the API and tinkered a bit. I finally came with something close to what I would like to achieve:


store.on('exception', function(storeProxy, type, action, options, response, args){
if(type == 'remote' && action == 'update'){
var erroneousObject = store.getById(response.raw.errPropId);
editor.startEditing(erroneousObject);
editor.showTooltip(response.message);
erroneousObject.reject();
}
});

and the error response from the server-side script issues the following:


{"success":false,"message":"Error trying to edit it!","errPropId":6}

What it does is to check if the type of error is a remote one (i.e success === false) and if the action is an update one then it gets the ID of the row the server-side script found to be incorrect, focus the editor on it and reject the change. To indicate the error I have used the showTooltip function.

The limitations of the above code are pretty obvious however. It will work with single updates but it will not be able to go through a set of errors. The second limitation is the showTooltip function -- it works fine for the current row edit, but any time I click on another item in the grid, it still shows with the last message supplied to the function as if the new item is invalid (which is not the case).

Perhaps there is a better way to achieve this? Does this look like an ugly code?:) I'm quite new to things .. yet.

Cheers!

ccyborg
6 Jan 2011, 1:05 PM
hello,

it seems that store exceptions or dataProxy exceptions are never catched when i do a store.save() with a remote error... o_0

i use extjs 3.3.1, it was the same with 3.3.0




Ext.data.DataProxy.on('exception', function(pProxy, pType, pAction, pException) {
alert(pType); // never displayed when server return {success:false}
});


... in another js class ...

,_SetActiveValue: function (pbActive) {
var oGrid = Ext.getCmp(EAIAdminControllers.MainGridId);
var b2Upd = false;
oGrid.getSelectionModel().each(function (poRs) {
poRs.set("Active", pbActive);
b2Upd = true;
});
if (b2Upd) {
var oSt = oGrid.getStore();
if (oSt.getModifiedRecords().length)
if (-1 != oSt.save()) {
// TODO FIX that : exception never thrown ???!!!
// store saved before remote data ???!!!!
}
}
}
}

....



an idea ?

ccyborg
11 Jan 2011, 8:49 AM
nobody ?

zetvio
15 Mar 2011, 10:55 AM
Hi,
If the server only return {success:false} it won't work.
You also have to include the data portion (same as in request), in your response.
Just have a look at this example http://dev.sencha.com/deploy/dev/examples/writer/writer.html.
Watch it in firebug and try to modify Fred Flintstone! The server will return:

{"success":false,"message":"SIMULATED ERROR: Lorem ipsum dolor sit amet, placerat consectetuer, nec lacus imperdiet velit dui interdum vestibulum, sagittis lectus morbi, urna aliquet minus natoque commodo egestas non, libero libero arcu sed sed.","data":{"id":1,"first":"Fred","last":"Flintstone","email":"[email protected]"}}