You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Ian MacLarty <ia...@gmail.com> on 2009/05/17 13:48:01 UTC

How do you get a custom FormComponent to "remember" erroneous values?

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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How do you get a custom FormComponent to "remember" erroneous values?

Posted by Ian MacLarty <ia...@gmail.com>.
On Mon, May 18, 2009 at 4:38 PM, Igor Vaynberg <ig...@gmail.com> wrote:
> In onbeforerender you should init the modelobkect not the model.
>
> But in your simple case you dont even need onbrforwrender, instead:
> new textfield(..., new propertymodel(this,"model")). Done.
>

Awesome. Thanks!

Ian.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How do you get a custom FormComponent to "remember" erroneous values?

Posted by Igor Vaynberg <ig...@gmail.com>.
In onbeforerender you should init the modelobkect not the model.

But in your simple case you dont even need onbrforwrender, instead:
new textfield(..., new propertymodel(this,"model")). Done.

-Igor

On Sunday, May 17, 2009, Ian MacLarty <ia...@gmail.com> wrote:
> 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: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How do you get a custom FormComponent to "remember" erroneous values?

Posted by Ian MacLarty <ia...@gmail.com>.
On Mon, May 18, 2009 at 2:07 PM, Martin Makundi
<ma...@koodaripalvelut.com> wrote:
> Does not look right... but who am I to say. Why do you call
> field.setModel(getModel()) at all?
>

I have to set the model of the nested component somewhere.  I can't
set it in the constructor, because the model is not available then.
The documentation for FormComponentPanel suggests doing it in
onBeforeRender.

> I would leave it out alltogether (= not change model). If you have
> other reasons to change the model, then you can override the
> FormComponent.internalOnModelChanged:
>
>                  /**
>                   * @see
> org.apache.wicket.markup.html.form.FormComponent#internalOnModelChanged()
>                   */
>                  @Override
>                  protected void internalOnModelChanged() {
>                    onValid(); // Do not clear input
>                  }

The docs say not to override this method, so I'd rather not do that.

I found that if I initialized the nested component in onBeforeRender
with the model (making sure I do it only once), then I got the
behaviour I wanted.  I don't understand why that works though.

Ian.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How do you get a custom FormComponent to "remember" erroneous values?

Posted by Martin Makundi <ma...@koodaripalvelut.com>.
Does not look right... but who am I to say. Why do you call
field.setModel(getModel()) at all?

I would leave it out alltogether (= not change model). If you have
other reasons to change the model, then you can override the
FormComponent.internalOnModelChanged:

                  /**
                   * @see
org.apache.wicket.markup.html.form.FormComponent#internalOnModelChanged()
                   */
                  @Override
                  protected void internalOnModelChanged() {
                    onValid(); // Do not clear input
                  }



**
Martin


2009/5/18 Ian MacLarty <ia...@gmail.com>:
> Hi Martin,
>
> On Sun, May 17, 2009 at 10:56 PM, Martin Makundi
> <ma...@koodaripalvelut.com> wrote:
>> ... raw input? In my understanding it works by default.
>>
>
> Thanks for the hint.  I got it to work by modifying my onBeforeRender to be:
>
>    @Override
>    protected void onBeforeRender() {
>        if (!field.hasRawInput()) {
>            field.setModel(getModel());
>        }
>        super.onBeforeRender();
>    }
>
> Could you confirm that this is the right fix?
>
> Ian.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How do you get a custom FormComponent to "remember" erroneous values?

Posted by Ian MacLarty <ia...@gmail.com>.
Hi Martin,

On Sun, May 17, 2009 at 10:56 PM, Martin Makundi
<ma...@koodaripalvelut.com> wrote:
> ... raw input? In my understanding it works by default.
>

Thanks for the hint.  I got it to work by modifying my onBeforeRender to be:

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

Could you confirm that this is the right fix?

Ian.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How do you get a custom FormComponent to "remember" erroneous values?

Posted by Martin Makundi <ma...@koodaripalvelut.com>.
... raw input? In my understanding it works by default.

	public final String getValue()
	{
		if (NO_RAW_INPUT.equals(rawInput))
		{
			return getModelValue();
		}
		else
		{
			if (getEscapeModelStrings() && rawInput != null)
			{
				return Strings.escapeMarkup(rawInput).toString();
			}
			return rawInput;
		}
	}


**
Martin

2009/5/17 Ian MacLarty <ia...@gmail.com>:
> 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: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org