This gets to be tricky stuff.

Remember that a form submissions starts with the page instance, with its
persisted fields rolled back to their latest values.  Anything that isn't
@Persist will be in a null or default state.

What's going on here is that your form is dependent on same transitory
state; that is, state that exists ephemerally during the rendering process.
That isn't a problem, except that the data you need is no longer available
when the form is submitted.

You could try to handle this much like an ActionLink, using the context
parameter of the form. That context is provided to the "prepare" event (i.e.,
the onPrepare() method).  The prepare event is fired as part of the initial
render, and at the start of the form submission.  In your case, the logical
context value would be the user name, or some form of user id.

In this way, you take direct control of the state information encoded into
your form, and provided back when the form is submitted.

On 9/28/07, Olof Næssén <[EMAIL PROTECTED]> wrote:
>
> I have a problem with using forms in components where the data model
> is passed to the components via parameter inside a T5 loop component.
> When a form is submitted the data model seems to be lost.
>
> What I'm trying to do is to iterate through a collection of users and
> construct a form for each user where a user name can be edited. Each
> form resides in a component that handles submission. The user model
> data is passed to the component inside a page where a loop loops
> through a collection of users. The idea is to have a form for each
> user so when submitting a form, only the user edited will get updated.
> However, when a form is submitted the user data model is lost and ends
> up as a null pointer.
>
> I wouldn't be surprised if I've missed something obvious or if the way
> I try to achieve a form for each user isn't a valid usage of T5. Any
> thoughts or suggestions? I've attached a simple example demonstrating
> my approach in this email.
>
> /Olof
>
> // User.java Data Model
> package org.example;
>
> public class User
> {
>     private String name;
>
>     public User(String name)
>     {
>         this.name = name;
>     }
>
>     public String getName()
>     {
>         return name;
>     }
>
>     public void setName(String name)
>     {
>         this.name = name;
>     }
> }
>
> //Users.java T5 Page
> package org.example.testapp.pages;
>
> import java.util.ArrayList;
> import java.util.List;
>
> import org.apache.tapestry.annotations.Persist;
> import org.example.User;
>
> public class Users
> {
>     private User user;
>
>     @Persist("flash")
>     private List<User> users;
>
>     public void onActivate()
>     {
>         if (users == null)
>         {
>             users = new ArrayList<User>();
>             users.add(new User("Ted"));
>             users.add(new User("Olof"));
>         }
>     }
>
>     public User getUser()
>     {
>         return user;
>     }
>
>     public void setUser(User user)
>     {
>         this.user = user;
>     }
>
>     public List<User> getUsers()
>     {
>         return users;
>     }
> }
>
> // Users.html T5 Template
> <html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd";>
> <head>
> </head>
> <body>
>         <t:loop source="users" value="user">
>                 <t:edituser user="user"/>
>         </t:loop>
> </body>
> </html>
>
> //EditUser.java T5 Component
> package org.example.testapp.components;
>
> import org.apache.tapestry.annotations.Parameter;
> import org.example.User;
>
> public class EditUser
> {
>     @Parameter(required = true)
>     private User user;
>
>     public void setUser(User user)
>     {
>         this.user = user;
>     }
>
>     public User getUser()
>     {
>         return user;
>     }
>
>     public void onSuccess()
>     {
>         // save user
>     }
> }
>
> //EditUser.html T5 Template
> <t:form xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd";>
>         <t:textfield value="user.name"/><br/>
>         <t:submit t:id="save" value="save"/>
> </t:form>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


-- 
Howard M. Lewis Ship
Partner and Senior Architect at Feature50

Creator Apache Tapestry and Apache HiveMind

Reply via email to