1

I have a model that can be described as follows:

http://aoeu.se/so/animal-dog-cat.png

(Extremely simplified. My actual classes have about ten-twenty more fields each.)

I have the persistence all worked out and I'm now trying to create a form in which users should be able to create objects of these classes (dogs and cats).

I would like it to look roughly as follows:

http://aoeu.se/so/new-animal-form.png

(I already have the HTML and the enabling / disabling of input fields implemented.)

To my question: How do I best implement the form processing? I don't see how I can directly use PropertyModels and such since the type of model object depends on the first Dog/Cat choice.

If Wicket guarantees that the order in which the fields are processed is the same as the order in which they appear in the web page, then I guess I could create the model object once the animal type input is processed (since it's the first form component), and let the remaining fields use a PropertyModel.

4
  • 1
    You could separate the Animal type selection from the rest of the form for example by Ajaxifying it and using the onChange Event to switch the Form Model to one matching the selected type (copying data where needed)...
    – Nicktar
    Commented Jul 15, 2013 at 9:23
  • Right. That would be another option. Would be an awful lot of copying though. But perhaps that's the best solution.
    – aioobe
    Commented Jul 15, 2013 at 9:30
  • If you'd use composition for the Models (like both the CatModel and the DogModel contain an AnimalModel and delegate methods) you could just move the 'inherited' part around...
    – Nicktar
    Commented Jul 15, 2013 at 9:32
  • That sounds like a neat idea. I'm new to Wicket however and have never composed models that way. Care to write an answer and elaborate a bit? (Oh, and one more thing: If the user enters "mjau" as the meowing sound, selects dog, and then reselects cat it would be nice if "mjau" was kept. Is this possible with the approach you are suggesting?)
    – aioobe
    Commented Jul 15, 2013 at 9:45

1 Answer 1

2

At first you should think about separating the animal type selection from the rest of the form as you'll need to discard some of the entries if the user decides to change the type after inputing some data. This can be archieved by ajaxifying this part of the form and using the onChange event to clean the data and switch the models.

When you do so, you might want to think about switching from inheritance to composition to avoid copying data around. I don't know if this is still compatible with your persistance strategy but you could allways copy the finished data to your persistance layer.

For example when your CatModel doesn't inherit from AnimalModel but instead contains an AnimalModel like this:

public class CatModel {

    private final AnimalModel parent;
    private String meowingSound;

    public CatModel(AnimalModel parent) {
        this.parent = parent;
    }

    public int getNumLegs() {
        return parent.getNumLegs();
    }

    public void setNumLegs(int numLegs) {
        parent.setNumLegs(numLegs);
    }

    public String getMeowingSound() {
        return meowingSound;
    }

    public void setMeowingSound(String meowingSound) {
        this.meowingSound = meowingSound;
    }

    public AnimalModel getParent() {
        return parent;
    }

}

(interfaces skipped)

You could take the AnimalModel from a (likewise constructed) DogModel to initialize your CatModels 'inherited' data.

public class AnimalModel {

    private int numLegs;

    public int getNumLegs() {
        return numLegs;
    }

    public void setNumLegs(int numLegs) {
        this.numLegs = numLegs;
    }
}

AnimalModel for completeness...

3
  • My model classes have lots of virtual methods and inner classes that rely on inheritance, so changing to composition is unfortunately not an option. (Unless you're talking about some auxiliary classes, but then I would have to duplicate each field in two different classes.)
    – aioobe
    Commented Jul 15, 2013 at 13:44
  • I think you'll have to stick to copying then. We're obiously using very different types of models. To me a model (for wicket) is little more than a data container.. some members, getters, setters, maybe some code to talk to daos, equals, hashCode and that's it for most cases.
    – Nicktar
    Commented Jul 15, 2013 at 13:56
  • I designed these classes long before I decided to create a web front-end and I'm reluctant to switch to anemic domain models.
    – aioobe
    Commented Jul 15, 2013 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.