-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4399091
By: dinglemouse
Hi, I am a new ZK user. I am having trouble with validation of a mandatory field.
I want to use my own Constraint class. It works fine until I reset fields to initial values (reset button). I'm using setRawValue(null) to set the username to null (which would otherwise fail my constraint)
The problem is *after* doing a username.setRawValue(null) the username.getValue() done by login button no longer seems to do any validation (???). I want
username.getValue() to fail validation when I press the login button always.
What did I do wrong?
Thanks!
Here is code to reproduce:
<?xml version="1.0" encoding="UTF-8"?>
<window title = "Validation Problem" border="normal" onCreate="doReset();">
<zscript>
/** Login. */
void doLogin() {
// getValue to do field validation
String userVal = user.getValue();
String passwordVal = password.getValue();
msg.value = "user=["+userVal+"],password=["+passwordVal+"]";
}
/** Reset fields. */
void doReset() {
user.setRawValue(null);
password.setValue(null);
msg.setValue(null);
}
/** My Validation Class. */
class MyMandatoryStringFld implements Constraint {
// Constraint
public void validate(Component comp, Object value) {
if (value == null || value.trim().length() == 0) {
String id = comp.getId();
StringBuffer sb = new StringBuffer();
sb.append("'").append(id).append("' is a mandatory field!");
throw new WrongValueException(comp, sb.toString());
}
}
}
MyMandatoryStringFld mandatoryStrFld = new MyMandatoryStringFld();
</zscript>
<vbox>
<html><attribute name="content"><![CDATA[
<pre>
Note the username is mandatory with my own custom constraint.
Step 1. Press Login button. Complains because user is mandatory. OK
Step 2. Press Reset. username is set (raw) to invalid null. OK
Step 3. Press Login button. This time the validation of username never occurs!
????
</pre>
]]></attribute></html>
<hbox>Username:<textbox id="user" constraint="${mandatoryStrFld}"/></hbox>
<hbox>Password:<textbox id="password" type="password"/></hbox>
<button label="Login" onClick="doLogin();"/>
<button label="Reset" onClick="doReset();"/>
</vbox>
<label id="msg"/>
</window>
Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4403414
By: jebberwocky
Dear dingleMouse
Not sure about why u put doReset() as following <window title = "Validation Problem" border="normal" onCreate="doReset();"> But, I don't think it is necessary putting doRest() on onCreate event. Because when window is created, the fields should be blank since u didn't declare any default value.
Try following:
<window title = "Validation Problem" border="normal" > ....
void doReset() {
user.setValue(null);
password.setValue(null);
msg.setValue(null);
}
.....
Jeff Liu
Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4403457
By: dinglemouse
Thankyou for your reply but it does not really address the problem I am having.
1. Perhaps the the doReset() is not necessary but AFAIK it is unrelated.
2. Suggesting user.setValue(null) instead of setRawValue(null) is no good because it will make reset violates my user constraint.
DM
Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4403709
By: henrichen
I tried this on ZK live demo and it works perfect. What ZK version are you using?
<window title="textbox with constraints" border="normal"> <attribute name="onCreate"> tbx.setRawValue(null); </attribute>
textbox: <textbox id="tbx" constraint="no empty"/> </window>
/henri
Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4404128
By: dinglemouse
The ZK version is 2.4.0.
Your sample works but it is not quite the same as the problem I am trying to describe.
Please try the code below instead.
How to reproduce:
1. Don't type anything into the textbox.
2. Press SHOW button. I expect the tbx.getValue() of the SHOW button would cause constraint violation (and it does the first time) 3. Presses RESET button
The REST confuses things and the press SHOW will not cause constraint violation now even though textbox is still empty.
How to avoid this unwanted side-effect of the txt.setRawValue(null)?
======
<window title="textbox with constraints" border="normal"> <attribute name="onCreate"> tbx.setRawValue(null); </attribute>
textbox: <textbox id="tbx" constraint="no empty"/>
<button label="RESET">
<attribute name="onClick">
tbx.setRawValue(null);
</attribute>
</button>
<button label="SHOW">
<attribute name="onClick">
msg.value = tbx.getValue();
</attribute>
</button>
Value of textbox: <label id="msg"/>
</window>
======
thanks.
DM
Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4405086
By: jebberwocky
Dear DM
I tried your sample.
According to ZK API, "[setRawValue] method is reserved for developer that really want to set an 'illegal' value (such as an empty string to a textbox with no-empty contraint)." Therefore, when setRawValue is used to set the value to blank, it might cause the constraint violation not being validated.
you might want to confirm this factor and work around it
Thank you
Jeff Liu
Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4407379
By: dinglemouse
IMO this is a bug because isValid() should return false when the field has invalid data regardless how it got there. Possibly related to a change "Feature 1461209"?
Anyway my workaround below is OK.
Thanks,
DM.
WORKAROUND.ZUL
====
<window title="textbox with constraints" border="normal"> <attribute name="onCreate"> tbx.setRawValue(null);
</attribute>
textbox: <textbox id="tbx" constraint="no empty"/> <button label="RESET"> <attribute name="onClick"> tbx.setRawValue(null); </attribute> </button> <button label="SHOW"> <attribute name="onClick"> String val = tbx.getValue();
tbx.validate(val); // WORKAROUND: Explicit validation required because invalid data gets past getValue() call
msg.value = val;
</attribute>
</button>
Value of textbox: <label id="msg"/>
</window>
=====
Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4463750
By: henrichen
Please post to Bug list along with your exmaple codes. Thanks.
/henri
<?xml version="1.0" encoding="UTF-8"?>
<window title = "Validation Problem" border="normal" onCreate="doReset();">
<zscript>
/** Login. */
void doLogin() {
// getValue to do field validation
String userVal = user.getValue();
String passwordVal = password.getValue();
msg.value = "user=["+userVal+"],password=["+passwordVal+"]";
}
/** Reset fields. */
void doReset() {
user.setRawValue(null);
password.setValue(null);
msg.setValue(null);
}
/** My Validation Class. */
class MyMandatoryStringFld implements Constraint {
// Constraint
public void validate(Component comp, Object value) {
if (value == null || value.trim().length() == 0) {
String id = comp.getId();
StringBuffer sb = new StringBuffer();
sb.append("'").append(id).append("' is a mandatory field!");
throw new WrongValueException(comp, sb.toString());
}
}
}
MyMandatoryStringFld mandatoryStrFld = new MyMandatoryStringFld();
</zscript>
<vbox>
<html><attribute name="content"><![CDATA[
<pre>
Note the username is mandatory with my own custom constraint.
Step 1. Press Login button. Complains because user is mandatory. OK
Step 2. Press Reset. username is set (raw) to invalid null. OK
Step 3. Press Login button. This time the validation of username never occurs!
????
</pre>
]]></attribute></html>
<hbox>Username:<textbox id="user" constraint="${mandatoryStrFld}"/></hbox>
<hbox>Password:<textbox id="password" type="password"/></hbox>
<button label="Login" onClick="doLogin();"/>
<button label="Reset" onClick="doReset();"/>
</vbox>
<label id="msg"/>
</window>
Asked: 2007-07-06 04:40:07 +0800
Seen: 1,567 times
Last updated: Nov 18 '11