You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Nathan Quirynen <na...@pensionarchitects.be> on 2013/05/03 09:59:13 UTC

Dynamically add fields to form

Hi,

I have in one of my pages the following:

  --Form A
|   Add item
  --------

--Form B
|    --Zone A
|   |  Item1
|   |  Item2
|    -----
|    Some other form fields
  --------

On success from Form A, Zone A gets updated and the new item added shows 
up in Form B. In Zone A each item has a select to change one of its values.
When adding a new item (Form A) I get the following error:

/Component ... must be enclosed by a Form component./

I know it's because of adding a form field dynamically to Form B. But 
how can I achieve what I'm trying? I have looked at AjaxFormLoop, but it 
doesn't look like what I need in my use case. Then I saw FormInjector, 
but I'm not sure how to use this.

Any hints/ideas on how to achieve what I described?

Thanks,
Nathan


Re: Dynamically add fields to form

Posted by George Christman <gc...@cardaddy.com>.
Hi Nathan, any particular reason your trying to use two forms?


On Fri, May 3, 2013 at 3:59 AM, Nathan Quirynen <nathan@pensionarchitects.be
> wrote:

> Hi,
>
> I have in one of my pages the following:
>
>  --Form A
> |   Add item
>  --------
>
> --Form B
> |    --Zone A
> |   |  Item1
> |   |  Item2
> |    -----
> |    Some other form fields
>  --------
>
> On success from Form A, Zone A gets updated and the new item added shows
> up in Form B. In Zone A each item has a select to change one of its values.
> When adding a new item (Form A) I get the following error:
>
> /Component ... must be enclosed by a Form component./
>
> I know it's because of adding a form field dynamically to Form B. But how
> can I achieve what I'm trying? I have looked at AjaxFormLoop, but it
> doesn't look like what I need in my use case. Then I saw FormInjector, but
> I'm not sure how to use this.
>
> Any hints/ideas on how to achieve what I described?
>
> Thanks,
> Nathan
>
>


-- 
George Christman
www.CarDaddy.com
P.O. Box 735
Johnstown, New York

Re: Dynamically add fields to form

Posted by Nathan Quirynen <na...@pensionarchitects.be>.
On 07/05/13 16:07, Lance Java wrote:
> Perhaps, as George mentioned, this is better handled by a single form with
> two submit buttons. You can use the onSelectedFromX() event to flag which
> submit button was pressed.
>
Hey,

I fixed my problem using an AjaxFormLoop in Form B:

1) Form A submit adds a new item to the collection and using 
JavaScriptSupport calls trigger() on the FormInjector inside the 
AjaxFormLoop
2) AjaxFormLoops onAddRow event returns the newly added item

Now the only problem I have left is: removing an item (using 
AjaxFormLoop remove event) works, but it does not rerender zones if I 
add some to this event (AjaxResponseRenderer.addRender()).

I'm using tapestry5-jquery library btw.

-- 

Een klare kijk op aanvullende pensioenen

*Nathan Quirynen*
03 340 04 60 | 0494 28 45 15
nathan@pensionarchitects.be <ma...@pensionarchitects.be>

Follow us on Web <http://www.pensionarchitects.be> | Twitter 
<http://www.twitter.com/pen_arch> | LinkedIn 
<http://www.linkedin.com/company/pension-architects> | RSS 
<http://feeds.feedburner.com/pensionarchitects> | YouTube 
<http://www.youtube.com/pensionarchitects>


Re: Dynamically add fields to form

Posted by Lance Java <la...@googlemail.com>.
Perhaps, as George mentioned, this is better handled by a single form with
two submit buttons. You can use the onSelectedFromX() event to flag which
submit button was pressed.

Re: Dynamically add fields to form

Posted by Nathan Quirynen <na...@pensionarchitects.be>.
Hi,

I'm trying this now, but I'm already stuck on rendering the 
FormInjector. Form A is submitted with ajax, then I want to rerender a 
zone containing the FormInjector. This always results in:

/No object of type org.apache.tapestry5.services.FormSupport is 
available from the Environment./

So I'm not sure on how to render the forminjector.

Nathan

On 03/05/13 18:36, Lance Java wrote:
> Disclaimer: I've never used FormInjector before
>
> I *think* you can do this but it will require a little trick. The
> FormInjector adds a trigger() function to it's DOM element. The trigger()
> function fires a serverside event passing the "context" attribute. In your
> use case, the context is not known until Form A is posted.
>
>   So, here's how I think you can do it:
>
>   1. User enters values and submits FormA
>
>
> 2. onSuccessFromFormA()
>
>                  2.1 Save the record to the DB and get an identifier
>
>                  2.2 Return a Block which renders a FormInjector
>
>                  2.3 Set the "context" of the FormInjector to the record id
>
>                  2.4 Use JavaScriptSupport to fire the FormInjector's
> trigger() event on the client
>
>
>
> 3. The FormInjector block renders clientside (zone is inside FormB)
>
>                  3.1 The javascript from 2.4 fires another serverside event
> passing the record identifier
>
>
>
> 4. Serverside event from trigger()
>
>                  4.1 Lookup the record from the DB using the context
>
>                  4.2 Return a Block containing a FormFragment
>
>
>
> 5. The block from 4.2 is inserted before/after the FormInjector
>


-- 

Een klare kijk op aanvullende pensioenen

*Nathan Quirynen*
03 340 04 60 | 0494 28 45 15
nathan@pensionarchitects.be <ma...@pensionarchitects.be>

Follow us on Web <http://www.pensionarchitects.be> | Twitter 
<http://www.twitter.com/pen_arch> | LinkedIn 
<http://www.linkedin.com/company/pension-architects> | RSS 
<http://feeds.feedburner.com/pensionarchitects> | YouTube 
<http://www.youtube.com/pensionarchitects>


Re: Dynamically add fields to form

Posted by Lance Java <la...@googlemail.com>.
Disclaimer: I've never used FormInjector before

I *think* you can do this but it will require a little trick. The
FormInjector adds a trigger() function to it's DOM element. The trigger()
function fires a serverside event passing the "context" attribute. In your
use case, the context is not known until Form A is posted.

 So, here's how I think you can do it:

 1. User enters values and submits FormA


2. onSuccessFromFormA()

                2.1 Save the record to the DB and get an identifier

                2.2 Return a Block which renders a FormInjector

                2.3 Set the "context" of the FormInjector to the record id

                2.4 Use JavaScriptSupport to fire the FormInjector's
trigger() event on the client



3. The FormInjector block renders clientside (zone is inside FormB)

                3.1 The javascript from 2.4 fires another serverside event
passing the record identifier



4. Serverside event from trigger()

                4.1 Lookup the record from the DB using the context

                4.2 Return a Block containing a FormFragment



5. The block from 4.2 is inserted before/after the FormInjector