I am running into a problem getting my forms working properly with my Formatters.

In my data model, all my objects derive from a base class that has a java.lang.Long "id" property, so I have TypeConverters and Formatters that work generally for everything that operate on this id property. So far so good. Two such model classes, lets call them "Foo" and "Bar" are shown here in skeletal form:

public class Foo {
    private Long id;
    private String name;

    /* getters and setters */
}

public class Bar {
    private Long id;
    private Foo foo;
    private String description;

    /* getters and setters */
}

The main thing to pay attention to is that Bar has a reference to a Foo, and that Foo is has both an id and a name (I'll get to that in a second).

When I create a form to create a new Bar, I set it up roughly like this:
<fieldset>
    <ol>
        <li>
            <stripes:label for="bar.id"/>
            <stripes:text name="bar.id"/>
        </li>
        <li>
            <stripes:label for="bar.foo"/>
            <stripes:select name="bar.foo">
                <stripes:option value="" label="None"/>
<%-- getAllFoos() returns all the Foo objects in the database --%> <stripes:options-collection collection="$ {actionBean.allFoos}" label="name" value="id"/>
            </stripes:select>
        </li>
        <li>
            <stripes:label for="bar.description"/>
            <stripes:text name="bar.description"/>
        </li>
    </ol>
</fieldset>

This works fine. However, I'm starting to get a lot of Foo objects in my database now, so a drop-down list is getting a bit cumbersome. I would like to transition from the <stripes:select> control to a <stripes:text> control that I've rigged up with an Ajax autocompleter that lets users start typing the name of the Foo they want to associate with the Bar object they're creating, and the autocompleter will bring back name choices from the database. I can use a text field instead of an options select to because I modified my TypeConverter to work with either an id or a name. I try parsing the input string as a Long first and if that works I do a lookup by ID in my database. If that parsing throws an exception, I catch it and treat the input string as the name, and do a lookup on that. This works just fine when I create a new Bar object, but I'm running into issues when I edit an existing Bar object, related to the fact that my general formatter Formatter always translates my model objects into the IDs. Thus, when I edit an existing Bar, the Formatter displays its ID in the text field, and not its name. Ideally, I'd like to be able to pass in a value to the <stripes:text> tag of ${bar.foo.name} but it looks like the <stripes:text> tag looks for a value in the ActionBean before it looks at either the value attribute or the body content of the tag.

I know I can create a new Formatter that operates on all Foo objects to use the "name" property instead of the "id" property and eliminate this problem, but I'd rather avoid that if at all possible, since I'd like all my URLs to be consistent (all use numbers, rather than some use numbers and others use names). Other ways I thought of tackling this are to just use HTML input fields instead of the Stripes tags and populate the value myself, although that means I won't get nice Stripes error messages for that field anymore. I suppose I could also use a stripes:hidden field with the id of the Foo, use an HTML input control to get and display the name, and have some JavaScript pass information back and forth, though that seems a tad baroque, un- Stripey, and broken without JavaScript.

Is there any other way to accomplish this, or am I horribly abusing Stripes here? Thanks for any tips or suggestions.

Chris

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Stripes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-users

Reply via email to