Problem with Data Binding and Constraint together

asked 2009-06-17 15:01:05 +0800

fmcypriano gravatar image fmcypriano
612 1 7


I've a page that is used to create a new Order, it's afterCompose creates a new order object and put it in the session. The NewOrder.zul is annotated to be binded to this session order. So far so good.

After I set constraint attributes to some components the afterCompose stops it's execution when the first component with constraint is "violated". For example, I'm creating a new order object and it's properties are all null the customer combobox's constraint is set to "no empty, strict" and it's the first component of the form.

<combobox model="@{customerList}" selectedItem="@{order.customer}" constraint="no empty, strict"/>
<combobox id="otherCombo" model="@{otherData}/>

In the afterCompose() I create a new order and fill the models to be used by the combos:

createNewOrder(); // order = new Order()

When createNewOrder() is executed the page shows the constraint error (the not empty message) and stops, both combos aren't filled by the model, if I remove the combobox's constraint everything is loaded correctly.

It shouldn't stop because of a new object. IMO the first load is supposed to show no errors and when the user start to enter the data the error should be appearing.

Are binding and constraint incompatibles? How could I solve this?

Felipe Cypriano

delete flag offensive retag edit

6 Replies

Sort by ยป oldest newest

answered 2009-06-17 17:55:50 +0800

fmcypriano gravatar image fmcypriano
612 1 7

A complete code that reproduces my problem:


import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.BindingListModel;
import org.zkoss.zkplus.databind.BindingListModelList;

import java.util.ArrayList;
import java.util.List;

public class TestComposer extends GenericForwardComposer{
    public void doAfterCompose(Component component) throws Exception {
        page.setVariable("newObject", null); // to simulate a property of a newly created object
        List list = new ArrayList();
        BindingListModel modelList = new BindingListModelList(list, false);
        page.setVariable("modelList", modelList);

        List list2 = new ArrayList();
        BindingListModel modelList2 = new BindingListModelList(list2, false);
        page.setVariable("modelList2", modelList2);


<?xml version="1.0" encoding="UTF-8"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>
    <window apply="TestComposer">
        <combobox id="first" model="@{modelList}"
                  selectedItem="@{newObject}" constraint="no empty, strict">
            <comboitem self="@{each=item}" label="@{item}"/>

        <combobox id="otherCombo" model="@{modelList2}">
            <comboitem self="@{each=item}" label="@{item}"/>

If you remove the constraint the code works, but it should work like this too, doesn't it?

Felipe Cypriano

link publish delete flag offensive edit

answered 2009-06-18 11:34:54 +0800

fmcypriano gravatar image fmcypriano
612 1 7

Hi Guys,

How I'm supposed to use constraints if isn't compatible with data binding? How do you are using it right now? This problem only happens with me?

link publish delete flag offensive edit

answered 2009-06-18 13:01:32 +0800

fmcypriano gravatar image fmcypriano
612 1 7

The problem seems to be that the constraint is getting applied when the binding is loading the object's data, and it should apply the constraints when saving the user input data, shouldn't it?

I think the behavior of textbox is what I told, replacing the combobox to textbox works fine and don't cause any problem.

link publish delete flag offensive edit

answered 2009-06-19 17:45:12 +0800

fmcypriano gravatar image fmcypriano
612 1 7

I found a solution, It's not what I want, but works.

Don't put the constraint in zul file, and when the user click on save button active all constraints and check them all:


        searchCliente.setConstraint("no empty")
        cmbLocalCobranca.setConstraint("no empty, strict")
        cmbCondicaoPagamento.setConstraint("no empty, strict")
        searchTransportadora.setConstraint("no empty")

        // Code to check them all

Feature Request to add a way to force the validation: ID: 2809002

link publish delete flag offensive edit

answered 2009-10-09 12:06:45 +0800

Matze2 gravatar image Matze2
773 7

Sorry to reactivate this old thread. I had the same problem today.
The reason why it works with textbox is, because there seems to be an explicit workaround for components which have their binding definition in a "value" attribute (as Textbox).
See class org.zkoss.zkplus.databind.Binding.myLoadAttribute:

//Bug #1876198 Error msg appears when load page (databind+CustomConstraint)
//catching WrongValueException no longer works, check special case and 
//use setRowValue() method directly
if ((comp instanceof InputElement) && "value".equals(_attr)) {

Would it be possible to do the same for combobox?

link publish delete flag offensive edit

answered 2020-02-27 21:27:32 +0800

Konditor gravatar image Konditor

Sets the constraint during onAfterSize:

<datebox id="db">                                        
    <attribute name="onAfterSize">
    db.setConstraint("no empty: "never empty beer mug");
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




Asked: 2009-06-17 15:01:05 +0800

Seen: 1,157 times

Last updated: Feb 27 '20

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