0

Problem with Data Binding and Constraint together

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

fmcypriano gravatar image fmcypriano
612 1 7
http://felipecypriano.com...

Hello,

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()
loadCustomerData();
loadOtherComboData();

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?


Regards,
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
http://felipecypriano.com...

A complete code that reproduces my problem:

TestComposer.java

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{
    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);
        page.setVariable("newObject", null); // to simulate a property of a newly created object
        
        List list = new ArrayList();
        list.add("a");
        list.add("b");
        BindingListModel modelList = new BindingListModelList(list, false);
        page.setVariable("modelList", modelList);

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

Test.zul:

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

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

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

Regards,
Felipe Cypriano

link publish delete flag offensive edit

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

fmcypriano gravatar image fmcypriano
612 1 7
http://felipecypriano.com...

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
http://felipecypriano.com...

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
http://felipecypriano.com...

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:

onClick:

        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
1

Sets the constraint during onAfterSize:

<datebox id="db">                                        
    <attribute name="onAfterSize">
    db.setConstraint("no empty: "never empty beer mug");
    </attribute>
</datebox>
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: 2009-06-17 15:01:05 +0800

Seen: 1,159 times

Last updated: Feb 27 '20

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