0

WrongValuesException and dependend values

asked 2010-04-30 03:01:34 +0800

xmedeko gravatar image xmedeko
1031 1 16
http://xmedeko.blogspot.c...

Hi,

I have a typical scenario:

<datebox id="datefrom" constraint="no empty" />
<datebox id="dateto" constraint="no empty" />
...
if (to.getValue().before(from.getValue()) {
  throw new WrongValuesException(new WrongValueException[] { /* error for datefrom */, /* error for dateto */ });
}

It displays two error boxes, that's really nice. But when the user changes just the value of "dateto", then it is still not possible to save the form, since the "datefrom" is still marked as wrong value (red border). Event if the user closes the error box shown for the "datefrom", then it still has the red border and the form is not possible to close. The user has to click into the "datefrom" datebox and then it is possible to save the form.

How to handle this situation? Is it possible to mark an input box as valid, when the user closes the error box?
So far, I am showing just an error Messagebox.

A workaround might be to close the error boxes in an onChange listener. But is also closes error boxes for "no empty" constraints.

Thanks

How to solve this problem

delete flag offensive retag edit

9 Replies

Sort by ยป oldest newest

answered 2010-05-04 06:37:22 +0800

iantsai gravatar image iantsai
2755 1

what about calling toBox.setValue(toBox.getValue()); to force trigger the validation again?

link publish delete flag offensive edit

answered 2010-05-04 08:41:55 +0800

madruga0315 gravatar image madruga0315 flag of Brazil
937 2 12

Another (complex) solution would be to implement your own constraint that handles both date fields.

link publish delete flag offensive edit

answered 2010-05-04 10:37:42 +0800

xmedeko gravatar image xmedeko
1031 1 16
http://xmedeko.blogspot.c...

@iantsai: Thanks, it might work. But it would be a little bit tedious to implement, since we have more such fields.

@madruga0315: How it can help? Do you mean I would have to call clearErrorMessage() for the other datebox in the complex Constraint implementation?

link publish delete flag offensive edit

answered 2010-05-05 09:26:46 +0800

xmedeko gravatar image xmedeko
1031 1 16
http://xmedeko.blogspot.c...

@madruga0315: Thanks, yeah, that's the proper solution, I have to call clearErrorMessage(true) for the other field in my own Constraint .

link publish delete flag offensive edit

answered 2010-06-07 10:57:39 +0800

xmedeko gravatar image xmedeko
1031 1 16
http://xmedeko.blogspot.c...

updated 2010-06-07 10:59:44 +0800

Hi guys, so event the clearErrorMessage(true) does not help me. Consider the following scenario:
1. datefrom value is "2010-06-06", dateto value is "2010-06-10"
2. the user sets the incorrect dateto as "2010-06-qq", the org.zkoss.zul.Datebox.coerceFromString(String) rises the WrongValueException and shows the user a message "You must specify a date ...". Now the value of dateto is still "2010-06-10", because the wrong string has not passed the coerceFromString() method.
3. the user changes datefrom to "2010-06-07", my validation calls dateto.clearErrorMessage(true), then a call dateto.getValue() returns "2010-06-10" and validation passes OK.
4. the user presses the OK button, my code just call datefrom.getValue(), dateto.getValue(), both passes OK, but the dateto returns "2010-06-10". So my code does not inform the user, that she has typed incorrect dateto, instead, it saves the old date to the database. The user is confused.

IMHO the validation is not designed well in the InputElement class. I think the raw value should be stored in some field "rawValue" and the constraint validation should be triggered each tome the getValue() is called.

link publish delete flag offensive edit

answered 2010-06-07 11:47:16 +0800

iantsai gravatar image iantsai
2755 1

Why not post it to feature request? I think ZK Team will think about it.

link publish delete flag offensive edit

answered 2010-06-08 01:33:56 +0800

xmedeko gravatar image xmedeko
1031 1 16
http://xmedeko.blogspot.c...

This is a common problem. I have hoped ZK has some solution to it.

link publish delete flag offensive edit

answered 2010-06-08 07:46:28 +0800

xmedeko gravatar image xmedeko
1031 1 16
http://xmedeko.blogspot.c...

I have, hopefully, found a working workaround. I do not use own Constraint, but I make dependent validations when the user click Save button (<button label="Save" onClick=...). The code is like:

String errmsg = this.dateto.getErrorMessage();
if (errmsg != null && Obejcts.equals(errmsg, this.dateto.getAttribute("wrongValueStored")) {
  this.dateto.clearErrorMessage(true);
}

if (this.dateto.getValue().before(this.datefrom.getValue())) {
  errmsg = "cannot be before date from" 
  this.dateto.setAttribute("wrongValueStored", errmsg);
  throw new WrongValueException(this.dateto, errmsg);
}

link publish delete flag offensive edit

answered 2010-06-11 01:05:08 +0800

xmedeko gravatar image xmedeko
1031 1 16
http://xmedeko.blogspot.c...

Feature Request ID: 3014680

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2010-04-30 03:01:34 +0800

Seen: 478 times

Last updated: Jun 11 '10

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More