0

Datebox: Custom parsing of the entered date

asked 2012-04-05 08:54:00 +0800

axenta gravatar image axenta
30 1

Hi there

I'm facing the following problem with dateboxes using ZK 5.0.5:
If the user enters an invalid date, the datebox shows an error message or, if the lenient attribute is set to true, it sets a similar date. Well, the parsing with lenient is too inflexible for my needs, so I'd like to parse the date by myself.
I would prefer to handle this on the serverside, because I already got a date parser in java. In this case I do not have to implement it again in javascript.

The problem is, I can't get around the client parsing. Is there a way to achieve this? Or do I have to write my own widget or override a javascript method?

Thanks in advance

delete flag offensive retag edit

1 Reply

Sort by ยป oldest newest

answered 2012-04-12 03:30:33 +0800

benbai gravatar image benbai
2228 6
http://www.zkoss.org

Hi,

Please refer to the sample:

ZKFiddle-Link

index.zul
<zk xmlns:w="client">
<zscript><![CDATA[
class CustomDatebox extends Datebox {
addClientEvent(CustomDatebox.class, "onCustomParse", CE_IMPORTANT|CE_DUPLICATE_IGNORE);
// override
public void service(org.zkoss.zk.au.AuRequest request, boolean everError) {
final String cmd = request.getCommand();
if (cmd.equals("onCustomParse")) {
String value = (String)request.getData().get("value");
if ("1".equals(value)) { // parse value to date
setValue(new Date(System.currentTimeMillis()));
smartUpdate("backToOriginal", "clear");
} else if ("2".equals(value)) { // parse value to date
setValue(new Date(System.currentTimeMillis() - (1000L * 60 * 60 * 24 * 365)));
smartUpdate("backToOriginal", "clear");
}
else // can not parse, use original client side value
smartUpdate("backToOriginal", "reset");
} else
super.service(request, everError);
}
}
]]></zscript>
<datebox lenient="true" use="CustomDatebox"
onCustomParse="">
<attribute w:name="doBlur_">
function (evt) {
// tmp store the old value
var oldValue = this.$n('real').value;
this.$doBlur_(evt);
// store the new value
this._newValue = this.$n('real').value;
// tmp set the old value to input
this.$n('real').value = oldValue;
// ask a custom parse from server
this.fire('onCustomParse', {value: oldValue});
}
</attribute>
<attribute w:name="setBackToOriginal">
function (v) {
// reset to original value as need then clear it
if (this._newValue) {
if (v == 'reset')
this.$n('real').value = this._newValue;
this._newValue = null;
}
}
</attribute>
</datebox>
</zk>

Regards,
Ben

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: 2012-04-05 08:54:00 +0800

Seen: 169 times

Last updated: Apr 12 '12

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