# MVVM automatic validation message binding

FlorianSchaetz
31 6

I've got a Macrocomponent that combines a label, a textbox and an error label...

<mycomponent id="productname" caption="Product name" value="Apple XYZ" error=""/>


...and now I want to use that for binding...

Obviously I could write...

<mycomponent id="productname" caption="Product name" value="@bind(vm.productName)" error="@bind(vmsgs['productname'])"/>


(given, of course, a validationMessages="@id('vmsgs') somewhere above and the apropriate binding annotations in the java code. All of this is already in place.)

...but of course, I would have to do this for each field and it would clutter my .zul files. So, is there any chance to somehow, perhaps even in java code, to tell the component to bind itself to vmsgs with the components id, without having to give that for every field? So that this would have the same effect...

<mycomponent id="productname" caption="Product name" value="@bind(vm.productName)"/>


..or perhaps

<mycomponent id="productname" caption="Product name" value="@bind(vm.productName)" bindError="true"/>


Of course, the component should stay usable in a non-MVVM environment, so adding fixed bindings is probably out. Any chance, for example, to find out in Java if we are bound somehow and add our own binding automatically?

To sumarize: I can already bind everything, no problem there. What I want to remove is the need of having to write explicitly error="@bind(vmsgs['productname'])" - this should somehow happen automatically.

delete retag edit

Sort by » oldest newest most voted

chillworld
5337 4 9
https://github.com/chillw...

I don't have tested it but when you have the Binder you could use addChildrenLoadBindings.

Of course now getting the binder :

comp.getAttributes().get("binder")


Just see that you can do it in the correct order and it should work.

Greetz chill.

I had some sucess with @ComponentAnnotation("@bind(vmsgs['keyword'])"), which only has the "problem", that the component that goes into the validation is the macro component, not the specific subcomponent, so the only "key" I get is not the sub-component id, but the ViewModel's property...

( 2016-09-02 18:26:46 +0800 )edit

Did you try tot reach the binder in aftercompose? Normally then you could add the binding to the correct component.

( 2016-09-02 18:38:16 +0800 )edit

It works for one macrocomponent, but as soon as I stack them in another, bigger mc, that one becomes the "source" for the validator, which means that I cannot (easily) have multiple "values" in one mc without some naming convention - instead of simply relying on id.

( 2016-09-02 19:00:35 +0800 )edit

Is it possible tot create a fiddle that combines 2 MC with that problemen? I see if I van check it out

( 2016-09-02 19:14:21 +0800 )edit

Thanks, will try. @ComponentAnnotation("@bind(vmsgs[self.id])") works quite fine. Problem now: My label+textbox+error mc could be used alone (then the BindContext.component.id would be the correct key for that) or inside another mc (then SavePropertyBinding.fieldName would be the correct key).

( 2016-09-02 19:36:52 +0800 )edit

maxspuls
1 2

Hi, in your component class you must put the following annotation

@ComponentAnnotation("error:@ZKBIND(ACCESS=both,SAVE_EVENT=on[Your Event])") public class [YourClass] extends ..... implements IdSpace{

EXAMPLE:

@ComponentAnnotation("value:@ZKBIND(ACCESS=both,SAVE_EVENT=onSetLabel)")

public class OasiLabel extends Label implements IdSpace{

public void setBindValue(String value) {
super.setValue(value);
Events.sendEvent("onSetLabel", this, null);
}


}

I am sorry, but while this is true, it has nothing to do with the question. This annotation allows me to bind fields to it, yes, true. But that already works. What I want to remove is the need to explicitly bind the validation message to it. I want it to be done automatically.

( 2016-09-02 16:03:51 +0800 )edit

ok I did not understand the problem :-)

( 2016-09-02 16:18:40 +0800 )edit
[hide preview]