Hi,

If I have a TextField whose model object is a Float, then if the user
enters an invalid value, say "aaa",  the TextField will remember that
invalid value and display it when the page is re-rendered.  How would
you go about getting the same behaviour for a custom FormComponent?

Here is my simple custom FormComponent.

public class MyPanel extends FormComponentPanel {
    TextField field;

    public MyPanel(String id) {
        super(id);
        field = new TextField("f", Float.class);
        add(field);
    }

    @Override
    protected void convertInput() {
        setConvertedInput(field.getConvertedInput());
    }

    @Override
    protected void onBeforeRender() {
        field.setModel(getModel());
        super.onBeforeRender();
    }
}

<wicket:panel>
    <input wicket:id="f" type="text" />
</wicket:panel>

And here is how I use it:

public class HomePage extends WebPage {

    private static final long serialVersionUID = 1L;

    public HomePage(final PageParameters parameters) {
        CompoundPropertyModel cpModel = new CompoundPropertyModel(this);
        Form form = new Form("form", cpModel);
        form.add(new TextField("field1", Float.class));
        form.add(new MyPanel("field2"));
        add(form);
    }

    private Float field1 = 1.0f;
    private Float field2 = 2.0f;

    public Float getField1() {
        return field1;
    }

    public void setField1(Float value) {
        field1 = value;
    }

    public Float getField2() {
        return field2;
    }

    public void setField2(Float value) {
        field2 = value;
    }
}

<html>
    <head>
        <title>Wicket Quickstart Archetype Homepage</title>
    </head>
    <body>
        <form wicket:id="form">
            <input wicket:id="field1" type="text" />
            <div wicket:id="field2" />
            <input type="submit" value="Submit" />
        </form>
    </body>
</html>

If I run this and enter "aaa" for field1 and "bbb" for field2 and
submit the form, then when the page is re-rendered field1 still has
"aaa", but field2 has been reset to "2".  I'm pretty sure the problem
is to do with the call to setModel in MyPanel#onBeforeRender, but I
don't know how else to link the FormComponent model with the model of
the TextField inside the FormComponent.

Any help would be greatly appreciated.  I'm using Wicket 1.3.5.

Cheers,
Ian.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to