1

I have a problem implementing dropdownchoice in my code. I want to display a list of object of type ProductCategory. This is all well and fine, but when i try to save the form, the whole ProductCategory object is saved, and not just the field from the object which is displayed in the select list.

Here is what my code looks like:

    IModel categories = new LoadableDetachableModel() {
        public List<ProductCategory> load() {
            List<ProductCategory> l = categoryService.findAllProducts();
            return l;
        }
    };

    IChoiceRenderer renderer = new IChoiceRenderer() {
        public Object getDisplayValue(Object obj) {
            ProductCategory category = (ProductCategory) obj;
            return category.getName();
        }

        public String getIdValue(Object obj, int index) {
            ProductCategory category = (ProductCategory) obj;
            return category.getName();
        }
    };

    DropDownChoice<ProductCategory> listCategories = new DropDownChoice<ProductCategory>(
            "productCategory",
            categories,
            renderer
    );

    add(listCategories);

The generated HTML looks something like this:

<select wicket:id="productCategory" name="productCategory">
    <option selected="selected" value="">Vælg en</option>
    <option value="test1">test1</option>
    <option value="test2">test2</option>
</select>

The "productCategory" field exists in an Object of type "Product", and is of type String.

As i tried to describe; i want to save ProductCategory.getName() to the "productCategory" field in Product, and not the entire ProductCategory Object. In other words: I want to save "test1" to Product.productCategory, but instead it saves com.test.webapp.domain.ProductCategory@1.

Can anyone please tell how this is done?

Any help is much appreciated.

Troels
  • 57
  • 5
  • Would it not make more sense to change the type of 'productCategory' to ProductCategoty? And do productCategory.getName() whenever you need the just the name? If yo realy want to just save the String, you must change from DDC to DDC .. – bert Mar 12 '12 at 13:15

1 Answers1

3

Your problem is that the model object behind the ddc is of type ProductCategory. On save, this will be casted to the String type - as defined in your model object behind the form.

I would change the code to have only strings in your choice list.

    public List<String> load() {
        List<String> pcChoices = new ArrayList<String>();
        for(ProductCategory pc : categoryService.findAllProducts()) {
            pcChoices.add(pc.getName());
        }
        return pcChoices;
    }

Doing so, you can also get rid of your choice renderer.

magomi
  • 6,599
  • 5
  • 31
  • 38
  • I had hoped that this would not be nessesary :/ Oh well, thanks for your answer :) – Troels Mar 12 '12 at 13:21
  • 1
    It's quite simple. If you want to set a `ProductCategory` value into a `ProductCategory` property, use a `DropDownChoice`, with an `IModel>` list model. If you want to set a `String` value into a `String` property, use a `DropDownChoice`, with an `IModel>` list model. – tetsuo Mar 12 '12 at 15:33
  • 1
    @tetsuo - If you want to use a list of strings then you have to provide a list of strings as choices for your ddc. But Troels asked if its possible to use a list of (domain) objects and only use a certain attribute of this object to be mapped into the model of the form. As far as I know this is not possible. So I think your answer is basically right but it misses the question a little bit ;) – magomi Mar 12 '12 at 16:27
  • @magomi It's is a comment, not an answer. Your answer is correct. I just wanted to point out that if you use a list of strings as choices, you must type the components and models as ``. – tetsuo Mar 12 '12 at 20:52
  • There is a case missing in that you may need to output strings but use another value for display. – Brill Pappin Mar 26 '14 at 22:16