You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Andrew Hall <an...@hotmail.com> on 2015/02/26 09:18:44 UTC

Using forms defined in panels

Hi all,
I have a newbie question ...
In the free online guide for Wicket - best practices section,2 listings are provided as examples of how to encapsulate components in panels:
(http://wicket.apache.org/guide/guide/bestpractices.html#bestpractices_1)
Listing 3:// Good component
public class RegistrationInputPanel extends Panel{
    public RegistrationInputPanel(String id, IModel<Registration> regModel) {
        super(id, regModel);
        IModel<Registration> compound = new CompoundPropertyModel<Registration(regmodel)
        Form<Registration> form = new Form<Registration>("form", compound);
        // Correct: Add components to Form over the instance variable
        form.add(new TextField("username"));
        form.add(new TextField("firstname"));
        form.add(new TextField("lastname"));
        add(form);
    }
}
Listing 4:public class RegistrationPage extends Page {
    public RegistrationPage(IModel<Registration> regModel) {
        Form<?> form = new Form("form");
        form.add(new RegistrationInputPanel("registration", regModel);
        form.add(new SubmitButton("register") {
            public void onSubmit() {
              // do something
            }
        });
        add(form);
    }
}
I infer from this example that it is possible in Wicket to decouple the physical definition of a form (listing 3) from the code which is executed when a form is submitted (listing 4). I suppose the point is that different pages can reuse the same physical form and implement their own form submission logic.
Is it possible for the "// do something" in listing 4 to access the values of "username", "firstname" & "lastname" submitted through the form defined in listing 3?
If the answer is yes, then could anyone provide a snippet of code demonstrating how to do this? I've had a search and have not found an obvious way!
Thanks,
Andrew. 		 	   		  

Re: Using forms defined in panels

Posted by Sven Meier <sv...@meiers.net>.
Hi,

the easiest solution is declare regModel as final, then you can "do something" as follows:
  
         form.add(new SubmitButton("register") {
             public void onSubmit() {
               // do something
               regModel.getObject().getUserName();
             }
         });

Note that declaring models as final might me dangerous, since the model 
is kept in the component hierarchy but nobody detaches it. This is not 
the case here though, since RegistrationInputPanel will take care of it.

Regards
Sven

On 26.02.2015 09:18, Andrew Hall wrote:
> Hi all,
> I have a newbie question ...
> In the free online guide for Wicket - best practices section,2 listings are provided as examples of how to encapsulate components in panels:
> (http://wicket.apache.org/guide/guide/bestpractices.html#bestpractices_1)
> Listing 3:// Good component
> public class RegistrationInputPanel extends Panel{
>      public RegistrationInputPanel(String id, IModel<Registration> regModel) {
>          super(id, regModel);
>          IModel<Registration> compound = new CompoundPropertyModel<Registration(regmodel)
>          Form<Registration> form = new Form<Registration>("form", compound);
>          // Correct: Add components to Form over the instance variable
>          form.add(new TextField("username"));
>          form.add(new TextField("firstname"));
>          form.add(new TextField("lastname"));
>          add(form);
>      }
> }
> Listing 4:public class RegistrationPage extends Page {
>      public RegistrationPage(IModel<Registration> regModel) {
>          Form<?> form = new Form("form");
>          form.add(new RegistrationInputPanel("registration", regModel);
>          form.add(new SubmitButton("register") {
>              public void onSubmit() {
>                // do something
>              }
>          });
>          add(form);
>      }
> }
> I infer from this example that it is possible in Wicket to decouple the physical definition of a form (listing 3) from the code which is executed when a form is submitted (listing 4). I suppose the point is that different pages can reuse the same physical form and implement their own form submission logic.
> Is it possible for the "// do something" in listing 4 to access the values of "username", "firstname" & "lastname" submitted through the form defined in listing 3?
> If the answer is yes, then could anyone provide a snippet of code demonstrating how to do this? I've had a search and have not found an obvious way!
> Thanks,
> Andrew. 		 	   		


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