# How can I bind an int value to radiogroup for selectedItem? It wants String?

rickcr
704 7

In my ViewModel I have an object that represents what I'm capturing data from the form for. (For simplicity using Person here)

The problem is one of the properties is an int that I need to bind to a radiogroup, but ZK seems to want a String to decide what value to set?

<radiogroup id="age" selectedItem="@bind(vm.person.ageRangeId)">


The radio button won't be set if "ageRangeId" is an int. It works if I have it a String. The somewhat easy way out is to bind the selected item to a vm "ageRangeAsString" and then in the getter/setter I do the conversion from/to the ageRangeId of the Person object. Seems a bit awkward though. Curious why ZK can't try to force the conversion (simply throw an exception if it fails.)

Am I missing something or am I correct that the selectedItem only works off of a string (when working off the value)?

delete retag edit

## 17 Replies

jj
638 3

Did you try Integer?

rdgrimes
735 7

When you bind to vm.person.ageRangeId, ZK actually looks for vm.getPerson().getAgeRangeId() and vm.getPerson().setAgeRangeId(). What does that tell you? You control the getter return type and setter argument type. So, even though your ageRangeId may be int, you can always create additional methods, such as getAgeRangeIdStr() and setAgeRangeIdStr() methods. You would then @bind to vm.person.ageRangeIdStr . You don't need an actual property called ageRangeIdStr because ZK won't look for one. It will only look for the getter and setter methods I described. Make sense? Then, your get/setAgeRangeIdStr() methods can handle the conversion between int and String in communicating the ageRangeId value back and forth.

hawk
2740 1 5
http://hawkphoenix.blogsp...

Hi, rickcr
Auto conversion from int to string should be a good for users. To deal with this problem, excepting adding a getter, you could implement a converter to convert int to string.

rickcr
704 7

@rdgrimes Typically yes you have control over the domain objects and you could add a getItemAsString/setItemAsString type of accessor... but in my opinion that often pollutes your domain objects and sometimes you are using a jar of domain objects that you don't even have pure control of - think of another department at the company. Yes, you could extend the class or create a wrapper class (adapter pattern) but it seems like a bit of a hack just to set a property that happens to use a different data type.

This is often a valid knock on Java.. sure pros cons exist I know. But when you are working things like groovy you typically don't have to worry so much about these kinds of things. I often prefer my front end to be loosely typed but the backend end value objects more strongly typed. On the front end it should try to coerce what it can at least for some basic number types.

I didn't want to pollute the domain object with a get/setString accessor so I opted to put that accessor in the view model itself and the accessor then works with the underlying domain object in the view model. (It's about 10 lines of code including braces that I really think could be done away with.)

rdgrimes
735 7

My suggestion was based on the reality of what you/we have to deal with. Ideally, yes, you are right. But, sometimes you have to sacrifice the ideal for the practical.

rickcr
704 7

"sometimes you have to sacrifice the ideal for the practical." I hear you rdgrimes. Thanks for offering the suggestion on the approach to take.

hawk
2740 1 5
http://hawkphoenix.blogsp...

Hi,
I think the converter (http://books.zkoss.org/wiki/ZK_Developer%27s_Reference/MVVM/Data_Binding/Converter ) is a way to keep your domain object clean and also fulfill your requirement.

rdgrimes
735 7

Wow! What a lot of code to accomplish such a simple thing.

rickcr
704 7

Ha yea. Converter is way overkill imo just for coercing an int to a string:) If there is a place to submit feature requests this should be one of them.

For now I'll just stick to a prop on the viewModel itself that gets/sets the int from/on the domain object...

public String getProdValTypeString() {
}

public void setProdValTypeString(String prodValTypeString) {
this.prodValTypeString = prodValTypeString;
//set the int property on the domain model:
}


hawk
2740 1 5
http://hawkphoenix.blogsp...

Hi,

another way is to bind radiogroup's model to an integer list and use template to create <radio>, bind radio's value to each object which is also a integer now. Then the selection will work correctly because ]vm.person.ageRangeId and each are both integer now.

<radiogroup id="age" selectedItem="@bind(vm.person.ageRangeId) model="@load(vm.ageList)">
<template name="model">
</template>


[hide preview]