You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Fabio Fioretti <wi...@gmail.com> on 2007/10/03 17:28:27 UTC

CompoundPropertyModel stops working when form validation fails.

Hi all,

thanks for your time and for keeping up this precious users' list.

I have a page with a page-global "currentRecommendation" variable. The
instance of this variable is updated by selecting one of the entries
of a ListChoice; basically, the ListChoice deals with a list of
"recommendations" instances and sets "currentRecommendation" to the
one selected by the user.

When a recommendation is selected, its data are loaded and displayed
in a "recommendationForm" (contained in the same page) defined like
this:

recommendationForm = new Form("recommendationForm");

The form contains a custom recommendationPanel which is linked to the
"currentRecommendation" instance by a CompoundPropertyModel built on
an AbstractReadOnlyModel, just like this:

IModel recommendationModel = new AbstractReadOnlyModel() {
    @Override
    public Object getObject(Component component) {
        return currentRecommendation;
    }
};

recommendationPanel = new CustomPanel("recommendationPanel", new
CompoundPropertyModel(recommendationModel));

The problem is that when the form is submitted and validation *fails*,
the CompoundPropertyModel of the "recommendationPanel" seems to
disconnect from the "currentRecommendation" instance, because clicking
on a different entry of the ListChoice mentioned above still refreshes
the form but doesn't update the data contained in it anymore.
In other words, the underlying "currentRecommendation" instance keeps
changing correctly on ListChoice selection change (and the
AbstractReadOnlyModel correctly picks up the selected instance), but
the form (i.e. the panel contained in it) keeps displaying the
instance that failed validation and doesn't show the newly selected
recommendation instance anymore.
The whole thing happens only when form validation fails.

What am I missing?


Thanks a lot for your help,

Fabio Fioretti - WindoM

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


Re: CompoundPropertyModel stops working when form validation fails.

Posted by anita nichols <an...@gmail.com>.
How do I use datagrid on wicket?
I tried the datagrid sample on wicket 1.2 is sooo confusing.

Any sugestion where to look

Re: CompoundPropertyModel stops working when form validation fails.

Posted by Fabio Fioretti <wi...@gmail.com>.
On 10/5/07, Eelco Hillenius <ee...@gmail.com> wrote:
> > *BUT*, when the user edits one of the recommendations making errors,
> > tries to save it and form validation fails, the panel's
> > CompoundPropertyModel seems to detach from the underlying
> > AbstractModel and, if the user selects another choice on the
> > ListChoice, the form is refreshed but it keeps showing data of the
> > recommendation instance that failed validation.
>
> Not sure what this could be. Would you be able to create a test case
> or quickstart project that shows the bug?

Given that calling form.clearInput() inside ListChoice's updating
behavior solves the problem, is it still useful to create a test case
to show CompoundPropertyModel's detachment or is it explainable now?
I'm still missing an explanation, but probably you are not (and in
this case, please share :-).


Thanks a lot,

Fabio Fioretti - WindoM

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


Re: CompoundPropertyModel stops working when form validation fails.

Posted by Eelco Hillenius <ee...@gmail.com>.
> *BUT*, when the user edits one of the recommendations making errors,
> tries to save it and form validation fails, the panel's
> CompoundPropertyModel seems to detach from the underlying
> AbstractModel and, if the user selects another choice on the
> ListChoice, the form is refreshed but it keeps showing data of the
> recommendation instance that failed validation.

Not sure what this could be. Would you be able to create a test case
or quickstart project that shows the bug?

Eelco

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


Re: CompoundPropertyModel stops working when form validation fails.

Posted by Fabio Fioretti <wi...@gmail.com>.
On 10/8/07, Kent Tong <ke...@cpttm.org.mo> wrote:
> It's a feature. When the selection changes, the user might have entered
> something into say a text field. Generally we don't want to lose that
> input (because all you want to do may be to show or hide a certain component
> depending on the selection), therefore it is saved and redisplayed to the
> user. By default it is clear only when the form is submitted and validation
> is passed successfully.

Ok, this behavior is understandable and fine. But when, after a failed
validation, the model object of the form is changed (by a new user's
selection on the ListChoice) and the form is refreshed, I'd expect it
to display the newly selected instance instead of the one that failed
validation. I mean, it's ok that the input isn't lost when validation
fails, but this should be limited to the object that actually failed
validation and it should't prevent using the form to manipulate
another instance after having changed form's model object accordingly.
In this sense, the feature looks like a limitation in my particular
case.

Of course I may be missing something, or not using the form as it is
meant to be used...


Thanks a lot,

Fabio Fioretti - WindoM

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


Re: CompoundPropertyModel stops working when form validation fails.

Posted by Kent Tong <ke...@cpttm.org.mo>.

Fabio Fioretti wrote:
> 
> You were right Kent, thanks a lot! Calling clearInput() in the
> updating behavior really fixes the problem.
> 
> What I miss now is why the CompoundPropertyModel breaks if the form is
> not manually reset by calling clearInput(). Is it a bug or a feature?
> :)
> 

It's a feature. When the selection changes, the user might have entered
something into say a text field. Generally we don't want to lose that
input (because all you want to do may be to show or hide a certain component
depending on the selection), therefore it is saved and redisplayed to the 
user. By default it is clear only when the form is submitted and validation 
is passed successfully.


-----
--
Kent Tong
Wicket tutorials freely available at http://www.agileskills2.org/EWDW
-- 
View this message in context: http://www.nabble.com/CompoundPropertyModel-stops-working-when-form-validation-fails.-tf4562483.html#a13092730
Sent from the Wicket - User mailing list archive at Nabble.com.


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


Re: CompoundPropertyModel stops working when form validation fails.

Posted by Fabio Fioretti <wi...@gmail.com>.
On 10/6/07, Kent Tong <ke...@cpttm.org.mo> wrote:
> Even though you're not using wantOnSelectionChangedNotifications(),
> what I said applies: you need to call clearInput() like:
[CUT]
> recommendationForm.clearInput(); //THIS LINE IS WHAT YOU NEED

You were right Kent, thanks a lot! Calling clearInput() in the
updating behavior really fixes the problem.

What I miss now is why the CompoundPropertyModel breaks if the form is
not manually reset by calling clearInput(). Is it a bug or a feature?
:)


Thank you for your time and precious help.


Kindest regards,

Fabio Fioretti - WindoM

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


Re: CompoundPropertyModel stops working when form validation fails.

Posted by Kent Tong <ke...@cpttm.org.mo>.

Fabio Fioretti wrote:
> 
> The ListChoice uses the same AbstractModel on which the panel inside
> the form builds its CompoundPropertyModel (see my previous posts):
> when user's selection changes, the AbstractModel correctly replaces
> its object with the newly selected instance and the form gets
> refreshed by an AjaxFormComponentUpdatingBehavior (attached to
> ListChoice's "onclick" event) to display the new instance (read from
> the same AbstractModel).
> 

Even though you're not using wantOnSelectionChangedNotifications(),
what I said applies: you need to call clearInput() like:

	ListChoice lc = new ListChoice("lc", ...);
	lc.add(new OnChangeAjaxBehavior() {
		protected void onUpdate(AjaxRequestTarget target) {
			recommendationForm.clearInput(); //THIS LINE IS WHAT YOU NEED
			target.addComponent(recommendationPanel);
		}
	});


-- 
View this message in context: http://www.nabble.com/CompoundPropertyModel-stops-working-when-form-validation-fails.-tf4562483.html#a13069881
Sent from the Wicket - User mailing list archive at Nabble.com.


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


Re: CompoundPropertyModel stops working when form validation fails.

Posted by Kent Tong <ke...@cpttm.org.mo>.

Fabio Fioretti wrote:
> 
> The ListChoice uses the same AbstractModel on which the panel inside
> the form builds its CompoundPropertyModel (see my previous posts):
> when user's selection changes, the AbstractModel correctly replaces
> its object with the newly selected instance and the form gets
> refreshed by an AjaxFormComponentUpdatingBehavior (attached to
> ListChoice's "onclick" event) to display the new instance (read from
> the same AbstractModel).
> 

Even though you're not using wantOnSelectionChangedNotifications(),
what I said applies: you need to call clearInput() like:

	ListChoice lc = new ListChoice("lc", ...);
	lc.add(new OnChangeAjaxBehavior() {
		protected void onUpdate(AjaxRequestTarget target) {
			recommendationForm.clearInput(); //THIS LINE IS WHAT YOU NEED
			target.addComponent(recommendationPanel);
		}
	});

--
Kent Tong
Free tutorials on Wicket at http://www.agileskills2.org/EWDW
-- 
View this message in context: http://www.nabble.com/CompoundPropertyModel-stops-working-when-form-validation-fails.-tf4562483.html#a13069882
Sent from the Wicket - User mailing list archive at Nabble.com.


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


Re: CompoundPropertyModel stops working when form validation fails.

Posted by Fabio Fioretti <wi...@gmail.com>.
On 10/5/07, Kent Tong <ke...@cpttm.org.mo> wrote:

Hello, thanks for your reply.

> I suppose you're using wantOnSelectionChangedNotifications() to trigger
> the refresh?

No, I'm not.

The ListChoice uses the same AbstractModel on which the panel inside
the form builds its CompoundPropertyModel (see my previous posts):
when user's selection changes, the AbstractModel correctly replaces
its object with the newly selected instance and the form gets
refreshed by an AjaxFormComponentUpdatingBehavior (attached to
ListChoice's "onclick" event) to display the new instance (read from
the same AbstractModel).

Everything works really smooth: the user selects a recommendation from
the list, he edits it, saves it, then he clicks on another
recommendation from the list and edits this other one, and so on.

*BUT*, when the user edits one of the recommendations making errors,
tries to save it and form validation fails, the panel's
CompoundPropertyModel seems to detach from the underlying
AbstractModel and, if the user selects another choice on the
ListChoice, the form is refreshed but it keeps showing data of the
recommendation instance that failed validation.

Any suggestion would be really appreciated, because maybe I'm missing
something about the "inner mechanisms" of Wicket.


Thank you all for any help,

Fabio Fioretti - WindoM

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


Re: CompoundPropertyModel stops working when form validation fails.

Posted by Kent Tong <ke...@cpttm.org.mo>.

Fabio Fioretti wrote:
> 
> This is what I do, basically:
> 1) Select a choice from the ListChoice;
> 2) The form automatically fills with the data of the selected
> recommendation instance;
> 3) Modify the data in the form so that validation will fail;
> 4) Submit data --> validation fails;
> 5) Select another choice from the ListChoice;
> 6) The form doesn't get updated anymore (and keeps showing old data of
> the recommendation instance that failed validation)
> 

I suppose you're using wantOnSelectionChangedNotifications() to trigger
the refresh? In that case it won't refresh any input entered by the user.
Validation plays no role here because such a form refresh does NOT invoke
the validation logic.

In order to refresh the data and erase what the user has entered, call
clearInput() on the form:

	ListChoice recommendation = new ListChoice("recommendation", ...) {
		protected boolean wantOnSelectionChangedNotifications() {
			return true;
		}
		protected void onSelectionChanged(Object newSelection) {
			recommendationForm.clearInput();
		}
	};

--
Kent Tong
Wicket tutorials freely available at http://www.agileskills2.org/EWDW
-- 
View this message in context: http://www.nabble.com/CompoundPropertyModel-stops-working-when-form-validation-fails.-tf4562483.html#a13057990
Sent from the Wicket - User mailing list archive at Nabble.com.


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


Re: CompoundPropertyModel stops working when form validation fails.

Posted by Fabio Fioretti <wi...@gmail.com>.
P.S.: I'm using Wicket 1.2.6.


Fabio Fioretti - WindoM

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


Re: CompoundPropertyModel stops working when form validation fails.

Posted by Fabio Fioretti <wi...@gmail.com>.
On 10/3/07, Martin-2 <vo...@gmail.com> wrote:
[ CUT on useful suggestions: thanks a lot ]
> The thing is: it's always better to rely on wicket for state handling and
> variable updating. So if two components refer to the same thing, make sure
> your model object is the same for both (or a property of the model object).
> This keeps your data where it belongs (to the model of MVC) and frees you
> from the hassle to deal with update events.

This is what I'm doing indeed: both the ListChoice (which, by the way,
is external to the form) and the panel inside the form are now sharing
the same AbstractModel defined like this:

final IModel recommendationModel = new AbstractModel(){
    public Object getObject(Component component) {
        return currentRecommendation;
    }

    public void setObject(Component component, Object object) {
        currentRecommendation = (Recommendation) object;
    }
};

The ListChoice uses the abstract model directly; the panel uses a
CompoundPropertyModel(recommendationModel) (i.e. over the abstract
one).

What I'm trying to understand is why, when form validation fails, the
CompoundPropertyModel seems to detach from the underlying
AbstractModel and doesn't pass new data to the panel anymore, even if
the AbstractModel correctly keeps getting updated on ListChoice
selection change (I tested it).

This is what I do, basically:

1) Select a choice from the ListChoice;
2) The form automatically fills with the data of the selected
recommendation instance;
3) Modify the data in the form so that validation will fail;
4) Submit data --> validation fails;
5) Select another choice from the ListChoice;
6) The form doesn't get updated anymore (and keeps showing old data of
the recommendation instance that failed validation)

If I come back to choice corresponding to the instance that failed
validation, modify the wrong fields and submit it again, everything
restarts working and the CompoundPropertyModel wakes up again.

> As a last resort, you could put the currentRecommendation to the session,
> the brutal php approach ;)

I'd like to avoid that approach. ;)

> Another thought... The form works until the first time validation failes.
> Check if subsequents submits go through onSubmit().

If I correct inserted data so that validation succeeds, yes,
onSubmit() is executed; otherwise, of course not.


Thanks again for your time and suggestions,

Fabio Fioretti - WindoM

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


Re: CompoundPropertyModel stops working when form validation fails.

Posted by Martin-2 <vo...@gmail.com>.
Ok, now I undestand better. So if the model of your page is complex, just put
the complex data in a custom model object and then use page.setModel(new
Model(complexObject)). Just move the get/set of your variable into the model
object.

In the form on submit then do

getPage().getModel().getObject().setCurrentRecommendation(selectedValue).

And the panel would be 

new CustomPanel("customPanel", new PropertyModel(getPage().getModel(),
"currentRecommendation");

As for refreshing, just replace 

setResponsePage(getPage())

with 

ajaxRequestTarget.addComponent(customPanel);

or whatever you'd like to be updated.

The thing is: it's always better to rely on wicket for state handling and
variable updating. So if two components refer to the same thing, make sure
your model object is the same for both (or a property of the model object).
This keeps your data where it belongs (to the model of MVC) and frees you
from the hassle to deal with update events.

As a last resort, you could put the currentRecommendation to the session,
the brutal php approach ;)

Another thought... The form works until the first time validation failes.
Check if subsequents submits go through onSubmit(). 

bw,
Martin


Fabio Fioretti wrote:
> 
> On 10/3/07, Martin-2 <vo...@gmail.com> wrote:
>>
>> Hi,
> 
> Hi, thanks for the quick reply.
> 
>> if I understand correctly, the page variable and the panel model should
>> always point to the same thing.
> 
> Right.
> 
>> I suggest you give your page a model with
>> currentRecommendation as model object. Then construct your panel with the
>> same model instance as the page (or wrap it with an IChainingModel) and
>> get
>> rid of the page wide variable.
> 
> I can't do that because my page is rather complex and it deals with at
> least two different kinds of objects. That's why I choose to have the
> currentRecommendation page variable instead, and a panel inside the
> related form that deals with the variable through the
> CompoundPropertyModel.
> 
>> The form's onSubmit() would be like:
>>
>> selectedValue = obtainSelectedValue();
>> getPage().getModel().setObject(selectedValue);
>> setResponsePage(getPage());
>>
>> so setting the page's model object to the choice the user selected, and
>> only
>> setting it if validation succeeds.
> 
> I don't think this could solve my problem because:
> 1) I don't want the whole page to be refreshed or reloaded but only
> the panel that deals with currentRecommendation;
> 2) more important, I don't want the panel to be refreshed only if
> validation succeeds; I want it to always reflect user's selection from
> the ListChoice, i.e. the currentRecommendation which is the model
> object of the panel.
> 
> The problem here is that, when form validation fails, the panel (which
> is contained in the form) stops responding to user's selections on the
> ListChoice as if its model loses the pointer to currentRecommendation.
> 
> I hope I've been clear enough, 'cause it's not so easy to explain...
> 
> 
> Thanks a lot for your help!
> 
> Fabio Fioretti - WindoM
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/CompoundPropertyModel-stops-working-when-form-validation-fails.-tf4562483.html#a13023833
Sent from the Wicket - User mailing list archive at Nabble.com.


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


Re: CompoundPropertyModel stops working when form validation fails.

Posted by Fabio Fioretti <wi...@gmail.com>.
On 10/3/07, Martin-2 <vo...@gmail.com> wrote:
>
> Hi,

Hi, thanks for the quick reply.

> if I understand correctly, the page variable and the panel model should
> always point to the same thing.

Right.

> I suggest you give your page a model with
> currentRecommendation as model object. Then construct your panel with the
> same model instance as the page (or wrap it with an IChainingModel) and get
> rid of the page wide variable.

I can't do that because my page is rather complex and it deals with at
least two different kinds of objects. That's why I choose to have the
currentRecommendation page variable instead, and a panel inside the
related form that deals with the variable through the
CompoundPropertyModel.

> The form's onSubmit() would be like:
>
> selectedValue = obtainSelectedValue();
> getPage().getModel().setObject(selectedValue);
> setResponsePage(getPage());
>
> so setting the page's model object to the choice the user selected, and only
> setting it if validation succeeds.

I don't think this could solve my problem because:
1) I don't want the whole page to be refreshed or reloaded but only
the panel that deals with currentRecommendation;
2) more important, I don't want the panel to be refreshed only if
validation succeeds; I want it to always reflect user's selection from
the ListChoice, i.e. the currentRecommendation which is the model
object of the panel.

The problem here is that, when form validation fails, the panel (which
is contained in the form) stops responding to user's selections on the
ListChoice as if its model loses the pointer to currentRecommendation.

I hope I've been clear enough, 'cause it's not so easy to explain...


Thanks a lot for your help!

Fabio Fioretti - WindoM

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


Re: CompoundPropertyModel stops working when form validation fails.

Posted by Martin-2 <vo...@gmail.com>.
Hi,

if I understand correctly, the page variable and the panel model should
always point to the same thing. I suggest you give your page a model with
currentRecommendation as model object. Then construct your panel with the
same model instance as the page (or wrap it with an IChainingModel) and get
rid of the page wide variable. The form's onSubmit() would be like:

selectedValue = obtainSelectedValue();
getPage().getModel().setObject(selectedValue);
setResponsePage(getPage());

so setting the page's model object to the choice the user selected, and only
setting it if validation succeeds. This is what we do in a similar case, we
got one listChoice that controls the whole page and so a lot of components.

hope that helps,
Martin


Fabio Fioretti wrote:
> 
> Hi all,
> 
> thanks for your time and for keeping up this precious users' list.
> 
> I have a page with a page-global "currentRecommendation" variable. The
> instance of this variable is updated by selecting one of the entries
> of a ListChoice; basically, the ListChoice deals with a list of
> "recommendations" instances and sets "currentRecommendation" to the
> one selected by the user.
> 
> When a recommendation is selected, its data are loaded and displayed
> in a "recommendationForm" (contained in the same page) defined like
> this:
> 
> recommendationForm = new Form("recommendationForm");
> 
> The form contains a custom recommendationPanel which is linked to the
> "currentRecommendation" instance by a CompoundPropertyModel built on
> an AbstractReadOnlyModel, just like this:
> 
> IModel recommendationModel = new AbstractReadOnlyModel() {
>     @Override
>     public Object getObject(Component component) {
>         return currentRecommendation;
>     }
> };
> 
> recommendationPanel = new CustomPanel("recommendationPanel", new
> CompoundPropertyModel(recommendationModel));
> 
> The problem is that when the form is submitted and validation *fails*,
> the CompoundPropertyModel of the "recommendationPanel" seems to
> disconnect from the "currentRecommendation" instance, because clicking
> on a different entry of the ListChoice mentioned above still refreshes
> the form but doesn't update the data contained in it anymore.
> In other words, the underlying "currentRecommendation" instance keeps
> changing correctly on ListChoice selection change (and the
> AbstractReadOnlyModel correctly picks up the selected instance), but
> the form (i.e. the panel contained in it) keeps displaying the
> instance that failed validation and doesn't show the newly selected
> recommendation instance anymore.
> The whole thing happens only when form validation fails.
> 
> What am I missing?
> 
> 
> Thanks a lot for your help,
> 
> Fabio Fioretti - WindoM
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/CompoundPropertyModel-stops-working-when-form-validation-fails.-tf4562483.html#a13021884
Sent from the Wicket - User mailing list archive at Nabble.com.


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