0

Constraints and databinding

asked 2008-08-27 12:45:41 +0800

dastultz gravatar image dastultz
797 9

Suppose I have a form and a list. The form is used to add items to the list. Each item in the list can be edited via a pop-up window upon clicking an edit button for the row. If the form has a textbox with a "no empty" constraint and it has the focus, clicking an edit button pops the edit window AND shows the constraint error message for the textbox. Me no likey. My form fields are bound to a bean. I am only concerned about valid field contents upon clicking an "Add" button for example. It seems the field contents are written to the bean with every onChange or whatever is appropriate for the field type. I would think that I could control this using the loadWhenEvents and saveWhenEvents of the DataBinder but I don't really understand the parameters.

How should I specify saveWhenEvent if I want the contents of fields to be written to the bean only when I call binder.saveAll()?

Thanks.

/Daryl

delete flag offensive retag edit

11 Replies

Sort by ยป oldest newest

answered 2008-08-27 14:38:05 +0800

robertpic71 gravatar image robertpic71
1275 1

Hi Daryl,

Here is an example for none event:

<textbox id="lastname" value="@{simpleWindow.PersonBean.lastname, save-when='none'}"/>

See the DataBinding API for more details.

See also this example from my homepage for the syntax.

I follow your threads with interest, because i also use databindig in most cases.

Actualy i do not use contraints with databinding. On my exerience (with other frameworks, casetools) i have always troubles with "simple contraints" defined in the GUI.

I do actualy use an utilclass to minimize the code - but i always searching for a better solution.

// ch = ContraintHandler(Window win);
ch.errMsgIfEmpty(receiver.getMail(), "to", "e-mail required");
ch.errMsgIf(receiver.getCopies() > config.getMaxCopies(), "copies", "max. 5 copies);

ch.errCdIfEmpty(receiver.getMail(), "to", "mail.empty");     // errCd: mail.empty will translated with Properties (multilingual)
...
cg.errMsg("content", "the are no times selected");
...

The ContraintHandler change the focus to the errorfield an throws a WrongValueExexption..not nice, but very straightforward.
Of course there is a lot space for improvments (i.e. default action: manage errors in hidden components)

/Robert

link publish delete flag offensive edit

answered 2008-08-27 18:18:11 +0800

dastultz gravatar image dastultz
797 9

Thanks Robert, that was a BIG help. One problem I have with this forum is that I develop Richlets rather than ZUL pages. I studied DataBinder rather than AnnotateDataBinder. :-)

I may consider writing my own constraint handler, I don't know. I like how the constraints are tied into the binder. How do you trigger your constraint handler?

/Daryl

link publish delete flag offensive edit

answered 2008-08-27 22:25:09 +0800

robertpic71 gravatar image robertpic71
1275 1

>> I may consider writing my own constraint handler, I don't know.
I've no chance to handle all my contraints with the GUI-contraints. I've to many dependencies between the GUI-fields, databases, securityroles.. so i need a way to output errormessages.

It depends on the application, i.e. it should no problem with the GUI-contraints to check an e-mail-form.

>> I like how the constraints are tied into the binder.
Of course this is pretty smart. But it should work without work-a-rounds:
i.e. no empty / focus problem
or errors in hiden components

>> How do you trigger your constraint handler?
There is no real trigger, but usual i create the contrainthandler with the window.

ContraintHandler ch = new ContraintHandler(window); 

In the onOK/onSave Event (after binder.save..) i do my checks like this:

ch.errMsgIf(bean.getValue() > config.getValue(), "guiId", "Error Message plaintxt");

The contrainthandler simple throws an exception (like the GUI-Contraints) - the look is the same.

Here some code from the handler:

private void errMsg(String componentName, String errMessage) {
	Component component = dftWindow.getFellow(componentName);
	InputElement inp = (InputElement) component;
	inp.focus();
	throw new WrongValueException(comp, errMessage);
}

To minimize code i have some variants like this:

public void errMsgIf(boolean error, String componentName, String errMessage) {
   if (error == false) {
      return;
   }
   errMsg(componentName, errMessage);
}

In your case (richlet), the custom contraint should not produce more lines.

/Robert (always looking for a better way..)

link publish delete flag offensive edit

answered 2008-08-28 01:08:05 +0800

dastultz gravatar image dastultz
797 9

Again, thanks for the details.

>> How do you trigger your constraint handler?
> There is no real trigger, but usual i create the contrainthandler with the window.

> ContraintHandler ch = new ContraintHandler(window);

> In the onOK/onSave Event (after binder.save..) i do my checks like this:

I think I get the constraint handler implementation, today I did a "one off" of constraint handling for my application, but I'm not quite getting all the options for hooking things together. Are you saying you have code, like on a Save button, that calls binder.saveAll() then you do your constraints on the bean, or are you attaching to onBindingSave or onBindingValidate somehow (I'm still studying the API)? I did my checks directly on the components (before binder.save...) I don't want to roll too much of my own solution if there's a better attaching point in Zk.

/Daryl

link publish delete flag offensive edit

answered 2008-08-28 01:25:15 +0800

dastultz gravatar image dastultz
797 9

I think for the most part I am happy with the simple constraints (and my type converters) preventing illegal values in fields. I prefer the approach we have been discussing for dealing with "no empty" checks (I changed my code to examine the bean rather than fields which is cleaner), and clearly it's the only way to deal with multi-field validation such as 2 dates where one has to be after the other. Maybe not the only way but pretty easy. What I'm lacking now is something my home-grown framework does - report on multiple bad fields at once. Suppose you hit save with 2 empty fields - it would be nice to decorate both fields instead of one, enter something, click save, then see warning for second field, enter something, then save again.

Have you addressed this at all?

/Daryl

link publish delete flag offensive edit

answered 2008-08-28 03:28:44 +0800

robertpic71 gravatar image robertpic71
1275 1

updated 2008-08-28 03:31:33 +0800

>> Are you saying you have code, like on a Save button, that calls binder.saveAll() then you do your constraints on the bean,...
Yes, that's my way. I also prefer to check the bean.

>> it would be nice to decorate both fields instead of one..
There is a clearErrorMessage for InputElement, but no setErrorMessage. I found only this Feature Request, but no resolution to do that.

I tried also creating an multiline-errorbox (MessageQueue like JSF) but there is no way to highlight more than one field at once...and the standard constraints (no empty) knows nothing about this errorbox...

See also: constraint docs

So i actually always decorate one field, SaveButton/Enter, next Field, SaveButton/Enter, next Field. Therefor i try always to handle onOK (Return key) as Save Button and focus to the errorfield.

/Robert

link publish delete flag offensive edit

answered 2008-08-28 12:13:22 +0800

dastultz gravatar image dastultz
797 9

>I tried also creating an multiline-errorbox (MessageQueue like JSF) but there is no way to highlight more than one field at once...and the standard constraints (no empty) knows nothing about this errorbox...

I have a multi-line error/feedback box now. Highlighting multiple fields should just be a matter of adding/removing the "text-invalid" css name to the fields. I can change the sclass easy enough but I've never seen multiple css names applied to elements the way Zk does it. I'll have to dig the source code for that...

/Daryl

link publish delete flag offensive edit

answered 2008-08-28 14:18:26 +0800

dastultz gravatar image dastultz
797 9

In my home-grown framework I have a validation phase. All validators are run and if any validator returns false, the next phase is cancelled. Zk handles this by throwing WrongValueException and interrupting the thread. I like this, except that it requires a component to be specified (to be decorated later). I'd like to validate multiple components at once, decorate them in my own way and still interrupt the thread. This is useful when you might have multiple "save" listeners that don't follow some API that requires them to return false if the thread should not proceed. Follow me?

There are multiple forms of WrongValueException. Is there any form that doesn't require a component, or is there some other exception that can be thrown, that will be caught and ignored by zk?

/Daryl

link publish delete flag offensive edit

answered 2008-08-29 07:46:58 +0800

henrichen gravatar image henrichen
3869 2
ZK Team

This might help for your case.

http://www.zkoss.org/javadoc/3.0.7/zul/org/zkoss/zul/CustomConstraint.html

link publish delete flag offensive edit

answered 2008-08-29 12:51:14 +0800

dastultz gravatar image dastultz
797 9

Yes, that would help for changing the way the component is decorated. But what I am looking for is a way to decorate multiple components AND interrupt the currently running thread. I suppose if I had three fields that failed a constraint, I could decorate the first two my own way (not using Constraint or WrongValueException), and then throw the exception for the last one. (Or I could decorate all three my own way, then throw an exception for any one of them and use a custom constraint where I don't decorate anything.)

/Daryl

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: 2008-08-27 12:45:41 +0800

Seen: 1,319 times

Last updated: Aug 30 '08

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