Search code examples
javatapestry

Unable to convert client value 'null' back into a server-side object


This question is about Tapestry components issue.

I'm looking for a solution of exactly this issue, not any workaround or alternative ways, how to implement this interface.

  1. Consider ajaxformloop element on the Tapestry form:

    <tr t:type="ajaxformloop" t:id="items" source="getItems()" value="item">

    ...

    </tr>

  2. getItems() method inside class returns synthetic combination (List interface) of persisted objects and not-yet-persisted newly added items (with null id inside them).

  3. When submitting the form I receive this error:

    Unable to convert client value 'null' back into a server-side object

This error occurs before onSuccessFromSave() method (save is id of submit link).

I wonder, how can I manage such not-persisted objects with ajaxformloop to prevent such error. Actually, I want to save (in DB) these items inside my onSucceccFrom...() method.

What have I missed here?


Solution

  • Actually I've missed custom ValueEncoder for my ajaxformloop container. The mentioned error produced by default encoder of this component.

    Custom encoder should be set in such way:

    <tr t:type="ajaxformloop" t:id="items" source="getItems()" value="item" encoder="itemEncoder">

    where itemEncoder is @Property annotated field in java class:

    @Property
    private ValueEncoder<MyItem> itemEncoder = new ValueEncoder<MyItem>() {
        @Override
        public String toClient(MyItem value) {
            return value.id != null ? value.id.toString() : "";
        }
    
        @Override
        public MyItem toValue(String clientValue) {
            if (clientValue != null && !clientValue.isEmpty()) {
                Long id = Long.parseLong(clientValue);
                return (MyItem) session.get(MyItem.class, id);
            }
            return new MyItem();
        }
    };