You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Damian Sobieralski <ds...@asu.edu> on 2007/03/02 00:22:40 UTC

Newbie and form submission (rewinding resetting values)

This hopefully is a trivial question but it is driving me crazy (more
so).

 I have a trivial form setup for Home.  Below is my Home.java

-----------------
import org.apache.tapestry.html.BasePage;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.annotations.InjectPage;
import org.apache.tapestry.IPage;
import org.apache.tapestry.form.StringPropertySelectionModel;

public abstract class Home extends BasePage implements
PageBeginRenderListener
{
	public abstract Person getPerson();
	
	public abstract void setPerson(Person person);
	
	public abstract StringPropertySelectionModel getGenders();
	public abstract void setGenders(StringPropertySelectionModel
model);

    public void pageBeginRender(PageEvent event)
    {
    	 if (! event.getRequestCycle().isRewinding()) 
    	 {
    	        setPerson(new Person());    	        
         } 
    	 
    	 StringPropertySelectionModel genders = new
StringPropertySelectionModel(new String[] {"M", "F"});
	        
	     setGenders(genders);


    }

    @InjectPage("Results")
    public abstract Results getResults();  
    
    public IPage doSubmit(IRequestCycle cycle)
    {
    	Results results = getResults();
    	
    	results.setPerson(getPerson());

    	return results;
    }	
}

-------------------------------

 The problem is that I am doing some validation.  The form is updating
the person's firstname, lastname, etc via a person POJO bean. I am using
beginPageRender to init the person object (new Person()).  Okay, no
problems.  And if I submit the form completely filled out, all works as
expected.

 However, since I am using the required validator, let's say I fill out
the person's first name but not their last name.  The page goes back to
Home and does "red" that those fields that are in error in the form. So
yay for that!  But it then resets all the form values (doesn't remember
them). So all my form fields (even the red ones) are all blank again
(like the user began the form all over again).

 I read (searched the tapestry-user archives) that some recommended in
beginPageRender to add the check to see if the page is being rewinded
(in this case, for an error).  However, when I do that after I submit
the page and validation errors send it back to home, I get a null
exception on the property (like person == null).  The code above is what
gives me the null property error when I submit on a validation error.
What happened to the person object?  Shouldn't a rewind still be part of
the pagecycle and thus still remember the submitted (al-be-it)
incomplete values for the form? Help!

 Thanks in advance for any assistance on this. Tapestry does things so
well and since it has such an easy way to do validation, I have to think
it has an easy way to remember values in an incomplete form. Gotta be
something stupid I am doing I say  :)

- Damian
  

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


Re: Newbie and form submission (rewinding resetting values)

Posted by Nick Westgate <ni...@key-planning.co.jp>.
Hi Damian.

The "set up" is only being done once: when the Person is null.
We create a new one to be edited, and we do this at the first
available opportunity, which is the render phase, since this
occurs before the first rewind.

(Keep in mind that the rewind phase is exactly the same as a render
phase except for (a) its purpose, which is to assign values back to
bound properties etc, and (b) that the HTML result is discarded.)

So that is a common pattern for initialization in pageBeginRender:
if (not rewinding)
{
     if (object1 is null)
     {
        init object1;
     }
     if (object2 is null)
     {
        init object2;
     }
     ...
}

The only other change was the @Persist annotation to tell Tapestry
to preserve the field's value. The default behaviour is for field
values to be discarded - we must specify which fields are stateful.

This default is reasonable since we want to minimize stored state.
Tapestry performs a lot of magic for us - we just have to remember
where it stops. ;-)

These concepts are no doubt covered in the first 4 free chapters of
Kent Tong's book. Check them out if you haven't read them yet.

Cheers,
Nick.


Damian Sobieralski wrote:
> Nick,
> 
>  Thank you!! That did the trick.
> 
>  Granted, I am new to this so this might be an asinine question - but
> why do I have to "set up" the form to be re-displayed on a validation
> error?  I mean, when would one rewind after a validation and NOT want
> the form values pre-filled in with their previously submitted values?
> Shouldn't that be the natural/default behavior and I'd only "overrule"
> this behavior if I had a reason?
> 
>  I understand that this is the way it works. I guess I'm just wondering
> why it was designed this way (probably something very reasonable that I
> am not aware of)?
> 
>  Anyways, this was most frustrating. But upto this point I was having
> fun with Tapestry.  So I can continue on with the learning.
> 
> Thanks again for scooting me on my way in the adventure!
> 
> - Damian

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


RE: Newbie and form submission (rewinding resetting values)

Posted by Damian Sobieralski <ds...@asu.edu>.
Nick,

 Thank you!! That did the trick.

 Granted, I am new to this so this might be an asinine question - but
why do I have to "set up" the form to be re-displayed on a validation
error?  I mean, when would one rewind after a validation and NOT want
the form values pre-filled in with their previously submitted values?
Shouldn't that be the natural/default behavior and I'd only "overrule"
this behavior if I had a reason?

 I understand that this is the way it works. I guess I'm just wondering
why it was designed this way (probably something very reasonable that I
am not aware of)?

 Anyways, this was most frustrating. But upto this point I was having
fun with Tapestry.  So I can continue on with the learning.

Thanks again for scooting me on my way in the adventure!

- Damian


> -----Original Message-----
> From: Nick Westgate [mailto:nick@key-planning.co.jp]
> Sent: Thursday, March 01, 2007 8:35 PM
> To: Tapestry users
> Subject: Re: Newbie and form submission (rewinding resetting values)
> 
> Hi Damian.
> 
> You need to add something like this here:
> +       @Persist
> > 	public abstract Person getPerson();
> 
> Otherwise the Person is not persisted between cycles.
> That's what's causing your current exception.
> 
> Also, you don't want to overwrite changes made to Person
> by the user, so you should add something here too:
> >     public void pageBeginRender(PageEvent event)
> >     {
> >     	 if (! event.getRequestCycle().isRewinding())
> >     	 {
> +            if (getPerson() == null)
> >     	        setPerson(new Person());
> >        }
> 
> Cheers,
> Nick.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org


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


Re: Newbie and form submission (rewinding resetting values)

Posted by Nick Westgate <ni...@key-planning.co.jp>.
Hi Damian.

You need to add something like this here:
+       @Persist
> 	public abstract Person getPerson();

Otherwise the Person is not persisted between cycles.
That's what's causing your current exception.

Also, you don't want to overwrite changes made to Person
by the user, so you should add something here too:
>     public void pageBeginRender(PageEvent event)
>     {
>     	 if (! event.getRequestCycle().isRewinding()) 
>     	 {
+            if (getPerson() == null)
>     	        setPerson(new Person());    	        
>        } 

Cheers,
Nick.

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