-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Good day.
Here is my zul
... <row>Email: <textbox value="@{view.person.email, save-when=saveButton.onClick}" readonly="@{view.readonly}" constraint="no empty"/></row> <button id="saveButton" label="Save"/> <button id="refreshButton" label="Refresh"/> ...
Button refresh is pressed when user wants to cancel all chages and reload object as it is in DB. But if user deletes text from email textbox and then he presses refresh button, validation message is shown near textbox.
How can I suppress binding validation for refreshButton and leave it only for saveButton?
Thanks, AlanSmith for your reply, but it does not help. Validation message appears not only on pressing refresh button, but even when textbox looses focus. And another thing: this control is binded to object's property and if I call clearErrorMessage, then after objects is refreshed and binder.loadAll() is called this controll does not get new (refreshed) value - it remains empty, while all other binded control's get new value.
Here's a pattern that might help:
1. Remove constraint="no empty" from ZUL
2. Add this to button click handlers where you want to validate:
textbox.setConstraint("no empty"); try { textbox.getValue(); } finally { textbox.setConstraint((String) null); }
The basic idea that you add the constraint when a button is clicked and try to get a value. If the validation fails, textbox.getValue() will throw a WrongValueException and a validation error will be shown. You must remember to remove the constraint in a finally block.
This approach reverses the validation so that by default there is no validation, and you validate manually in onClick listeners.
If you use this approach, there might be no point in using constraints at all. You can plug in any custom validation code if you just throw a WrongvalueException when a validation error happens. So the above code could also be expressed like this:
if (StringUtils.isBlank(textbox.getValue)) throw new WrongValueException(textbox, "Email cannot be empty");
Personally I prefer this approach because I can customize all the validation messages for every field and I can plug in any custom validation code without using ZK constraints.
Thanks for your advise.
I prefer using Spring+Custom Validators. Real code looks like
Zul
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?> ... <datebox value="@{view.person.dateOfBirth, save-when='saveButton.onClick'}" readonly="@{view.readonly}" constraint="no empty, no future"/> <textbox id="emailText" value="@{view.person.email, save-when='saveButton.onClick'}" readonly="@{view.readonly}" constraint="${emailConstraint}"/> ... <button id="refreshButton" label="Refresh"/>
EmailConstraint.java
@Component("emailConstraint") public class EmailConstraint implements Constraint { @Override public void validate(org.zkoss.zk.ui.Component comp, Object value) throws WrongValueException { String email = (String) value; if (email == null || !EmailValidator.getInstance().isValid(email)) { throw new WrongValueException(comp, "Incorrect email"); } } }
The problem is that binded control do not reload it's property it is has validation message. If in above example you change datebox to correct date and change email to incorrext, then on pressing refreshButton datebox loads new value from binded object, while emailText don't - it shows old value with validation message.
public void onClick$refreshButton() { presenter.refresh(); //here is loading new "person" object. binder.loadAll(); }
Please, help me do disable validation in this onClick listener.
Asked: 2011-05-03 10:54:10 +0800
Seen: 571 times
Last updated: May 05 '11