ljf0030 / gwt-pectin

Automatically exported from code.google.com/p/gwt-pectin
0 stars 0 forks source link

ValidationPlugin.validateField(x).using(y).when(z) react on on changes on z one step to late #37

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I have two textboxes, which depend one from the other.
Only then text in one, the other activate and a need field change.

<ui:msg description="login username">Benutzername</ui:msg><br />
<pectin:EnhancedTextBox addStyleNames='{style.loginTextBox}' 
ui:field="username" /><br />
<g:Label addStyleNames='{style.validationMessage}' 
ui:field="usernameValidationMessage" /></p><p>
<ui:msg description="login password">Passwort</ui:msg><br />
<pectin:EnhancedPasswordTextBox addStyleNames='{style.loginTextBox}' 
ui:field="password" /><br />
<g:Label addStyleNames='{style.validationMessage}' 
ui:field="passwordValidationMessage" />

Model:
username = fieldOfType(String.class).boundTo(provider, "username");
password = fieldOfType(String.class).boundTo(provider, "password");

username.addValueChangeHandler(new ValueChangeHandler<String>() {
public void onValueChange(ValueChangeEvent<String> event) 
{triggerValidate();}});

password.addValueChangeHandler(new ValueChangeHandler<String>() {
public void onValueChange(ValueChangeEvent<String> event) 
{triggerValidate();}});

MetadataPlugin.enable(password).when(Conditions.textOf(username).isNotBlank());
MetadataPlugin.watermark(username).with(loginConstants.usernameWatermark());

ValidationPlugin.validateField(username).using(new 
NotEmptyValidator(loginConstants.emptyUserName()));
ValidationPlugin.validateField(password).using(new 
NotEmptyValidator(loginConstants.emptyPassword())).when(Conditions.textOf(userna
me).isNotBlank());

When i type one char into the username the password field activate, but no 
validation starts on the field, the validation starts then i type a another 
char into user

The change event comes first for the change of textfield, the delegator caches 
the delegation value and become the old no value, then the change event for the 
delegator come and change the delegator value, then the change will fire, but 
no listener is listen to this event, so the validation become the change by the 
next change event 

Pectin 0.8
GWT 2.1 SVN (Build 2010-09-08 16:00)
SUN Java 1.6.0_21
SuSE Linux 11.3 (32bit)
Firefox 3.5.11

Original issue reported on code.google.com by woc...@gmail.com on 13 Sep 2010 at 6:56

GoogleCodeExporter commented 8 years ago
I think this problem is a side effect of:
a) Event ordering (i.e. GWT event handlers fire in the order they are added).
b) Conditions cache their values hence only update on value change events 
(instead of recomputing everytime getValue() is called).

So in the above example triggerValidate() will execute before the textOf(..) 
condition has recomputed based on the change.

The simplest solution in this case (if I'm correct) is to add your handlers 
that trigger validation after you've configured your validators.  I would 
recommend documenting it in the comments (c:

Another option is to execute the validation step using a DeferredCommand, but 
I'd only do this as a last resort as you loose control over when the validation 
happens.

This is a bit of a Gotcha with event programing, so I should definitely add it 
to the Wiki.

Let me know if this is the issue and I'll close it.

Cheers
Andrew

Original comment by andrew.pietsch on 14 Sep 2010 at 4:06

GoogleCodeExporter commented 8 years ago
Thanks for your great answer, 

a) was the problem, when i moved the addValueChangeHandler behind the 
validateField( it works correct.

Greet Rainer

Original comment by woc...@gmail.com on 14 Sep 2010 at 6:54

GoogleCodeExporter commented 8 years ago
No worries.  I'll close this issue.

Cheers
Andrew

Original comment by andrew.pietsch on 14 Sep 2010 at 7:40