Wednesday, June 06, 2007

Associating Wicket feedback messages with a given form on a page

I ran into a situation where I can have more than one form on my page and I want the error messages to appear above the relevant form. However, I couldn't use ComponentFeedbackPanel to achieve the right filtering. The following did not work as I'd hoped:
form.add(new ComponentFeedbackPanel("feedback", form));
...
form.add(new EqualPasswordInputValidator(password1, password2));

However, by subclassing FeedbackPanel it was easy to create a useful, reusable FormFeedbackPanel:
public class FormFeedbackPanel extends FeedbackPanel
{
public FormFeedbackPanel(String id, final Form form)
{
super(id, new ComponentFeedbackMessageFilter(form)
{
@Override
public boolean accept(FeedbackMessage message)
{
return message.getReporter() == form
|| form.contains(message.getReporter(), true);
}
});
}
}

To use it, you can attach validators as above, or call
 form.error("Your error message here");

Thanks to Jonathan Locke for the suggestion.

3 comments:

robottaway said...

Hehe, I was just thinking about how I would solve this. Your solution is about what I was imagining. Thanks for saving me some time!

Muhamed said...

You are the man!
Helped me on a project that carries 50% of my mark...THANKS!

Facundo said...

Hello, do you know how to assigne css styles or ohter way to put format in this message?

Thanks