PDA

View Full Version : Entering an invalid value in a bound field does not show an error message



ivantay
18 Aug 2009, 5:11 AM
Try in the Ext GWT Explorer Demo/Forms/Forms: insert a invalid date in the
Birthday field and press tab: the field is marked as invalid and you
get the error message for the invalid date.

Now try the same in the Binding/Basic Binding example (Updated field):
The field contains a valid date (today). Try entering an invalid value
(eg: 8/33/09) and press tab: you don't get any error.

After some digging I found that this is due to the fact that
the Field's onBlur method always fires a change event, even if the
field is not valid.
If the field is bound, the FieldBinding catches the event and updates
the model with null.
The field is then validated again with the new value (null), the result
is that the invalid state and the error message are cleared, since
null is a valid value.

Is this the correct way?
Usually, if a user enters a invalid value, I would like my app to tell
the user that the input is invalid, instead of updating the model with null.


I first wrote a workaround for the FieldBinding, ignoring the change event
if the field is invalid:



bindings.addFieldBinding(new FieldBinding(date1, "date1") {
@Override
protected void onFieldChange(FieldEvent e) {
//if (getField().isValid())
if (getField().getErrorMessage() == null) // does not require another validation
super.onFieldChange(e);
}
});
but this has to be applied to all fields.

What about changing the Field's onBlur event firing the event only if the field is valid?
Something like:



protected void onBlur(ComponentEvent be) {
boolean validated = true;
if (!GXT.isOpera && focusStyle != null) {
getInputEl().removeStyleName(focusStyle);
}
hasFocus = false;
if (validateOnBlur) {
validated = validate();
}

Object v = getValue();

if ((focusValue == null && v != null) || (focusValue != null && !focusValue.equals(v))) {
if (validated) fireChangeEvent(focusValue, v);
}
fireEvent(Events.Blur, new FieldEvent(this));
}