PDA

View Full Version : Synchronous calls, is it possible?



estevaofreitas
17 Aug 2009, 3:41 AM
Hi all, I just wondering if is possible to perform a synchronous call.

Some of you may thing I'm a crazy man, but sometimes I think it's a need.
For example, when I validate a field through a server call, I just want to perform a call, handle the reponse and validate the field. The way RPC proxy uses callbacks I can't use a field Validator because I have the response later.

Any ideas?
I wish a nice week for all.

Estevão Freitas

ror
19 Aug 2009, 1:35 AM
Hi,
You can't use synchronous call in GWT. When you need this feature you must in time of
asynchronous call block UI (method mask()) and when you recevie and process response simple unblock (unmask()).
Need some example code?

Colin Alworth
19 Aug 2009, 3:47 AM
Be careful 'trusting' your server to do validation - it sounds almost as though you are using this as a mechanism to make sure you trust user input, and that way lies security holes.

If it is just a matter of providing some additional feedback to the user, then I would stick with the Validator approach, but if client can't find a reason to return false, then fire the RPC call (and maybe start a spinner to indicate work being done) and return true. Then if the server says that the input is not valid, call Field.forceInvalid(String) from the async handler.

siberian
20 Dec 2009, 7:11 PM
Sorry too flog this thread but this pattern worked well for me and I thought future searchers may want to know. Thanks!


public class EmailValidator implements Validator {

public String validate(Field<?> field, String value) {
if (!((String) field.getValue())
.toLowerCase()
.matches(
"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])")) {
return "Bad E-mail Address";
}
RemoteService.Util.getInstance().isEmailAvailable((String) field.getValue(), new UniqueEmailCheckCallback(field));
return null;
}
}


and then in my Callback:


public class UniqueEmailCheckCallback<T> implements AsyncCallback<T> {
Field theField;

public UniqueEmailCheckCallback(Field<?> field) {
theField = field;
}
public UniqueEmailCheckCallback(){
GWT.log("Generic email validator constructor. This should not be called!",null);
}
public void onFailure(Throwable caught) {
GWT.log("Email is not unique", null);
theField.forceInvalid("This email address is already in use.");
}

public void onSuccess(T result) {
GWT.log("Email IS unique", null);
}
}

siberian
20 Dec 2009, 7:39 PM
Whoops should be



theField.markInvalid("This email address is already in use.");


forceInvalid stops all additional validation.

(also updated parent thread)