0

How to ignore constraint

asked 2011-05-03 10:54:10 +0800

Arsen gravatar image Arsen
384 5

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?

delete flag offensive retag edit

6 Replies

Sort by ยป oldest newest

answered 2011-05-03 10:55:25 +0800

Arsen gravatar image Arsen
384 5

My Zk version is ZK 5.0.4 CE (build: 2010090312)

link publish delete flag offensive edit

answered 2011-05-04 08:39:19 +0800

AlanSmith gravatar image AlanSmith
39 1

give your <textbox> an id, and in your GenericForwardComposer do the following when refreshButton is pressed


idGivenToTheTextbox.clearErrorMessage();

link publish delete flag offensive edit

answered 2011-05-04 09:29:34 +0800

Arsen gravatar image Arsen
384 5

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.

link publish delete flag offensive edit

answered 2011-05-04 09:53:12 +0800

gekkio gravatar image gekkio flag of Finland
899 1
http://gekkio.fi/blog

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.

link publish delete flag offensive edit

answered 2011-05-05 08:08:28 +0800

Arsen gravatar image Arsen
384 5

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");
        }
    }
}

It helps to avoid extra coding.

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.

link publish delete flag offensive edit

answered 2011-05-05 08:31:14 +0800

Arsen gravatar image Arsen
384 5

Followed code helps

public void onClick$refreshButton() {
        presenter.refresh(); //here is loading new "person" object.
        emailText.clearErrorMessage(true);
        self.invalidate();
    }

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: 2011-05-03 10:54:10 +0800

Seen: 571 times

Last updated: May 05 '11

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