adding elemets to zend_form dynamically

9 messages Options
Embed this post
Permalink
KimG

adding elemets to zend_form dynamically

Reply Threaded More More options
Print post
Permalink
Hi.

i'm trying to use zend_form and subform to construct a user input form that allows the user to add new rows.

what i've started out with is this:

form creation (details left out)

$numbers = new Zend_Form_SubForm();
$numbers->setIsArray(true);
$numbers->addElement('text', '1', array(
    'label' => 'Number 1',
    'required' => true,
));
$numbers->addElement('text', '2', array(
    'label' => 'Number 2',
    'required' => true,
));
$form->addSubForm($numbers, 'numbers');


which basically transforms to this:

<input type="text" name="numbers[1]" id="numbers-1" value="6" />
<input type="text" name="numbers[2]" id="numbers-3" value="4" />


when i submit the form the values 4 and 6 are returned to my action as expected.

but if the user adds another row giving this:

<input type="text" name="numbers[1]" id="numbers-1" value="6" />
<input type="text" name="numbers[2]" id="numbers-3" value="4" />
<input type="text" name="numbers[3]" id="numbers-2" value="5" />


i still see only 2 values 6 and 4.

i know for sure that all three values are posted to my action because doing this:

  foreach ($_POST['numbers'] as $row) {
               echo $row;
}

gives 6 4 and 5.

but how come zend doesn't see this extra value?

is it possible at all to use zend_form for this kind of tricks?

thanx in advance.

kim
prodigitalson

Re: adding elemets to zend_form dynamically

Reply Threaded More More options
Print post
Permalink

KimG wrote:
gives 6 4 and 5.

but how come zend doesn't see this extra value?
Think about it... :-)

When the form is posted youre getting raw data which is used to reconstruct the form so you have a form object to work with. Generally you do this using the same construction process first used. In order to get new values you have to add those fields when you reconstruct the form.
So you need to loop through the data and create fields instead of explicitly creating them when youre processing a posted form - perhaps when you first create the form as well for consistency or if you are putting the code in a custom form class or using the same action that produces the inital view of the form.
KimG

Re: adding elemets to zend_form dynamically

Reply Threaded More More options
Print post
Permalink
In reply to this post by KimG
Maybe i wasn't explicit enough.

The user adds the extra row on the client using jscript and DOM.

I would like to avoid having to take another round trip to the server for adding an extra row.

I guess that's what you mean i have to do in order for zend to know that an extra row has been added.

kim
KimG

Re: adding elemets to zend_form dynamically

Reply Threaded More More options
Print post
Permalink
In reply to this post by KimG
thanx for your input but maybe i wasn't explicit enough.

The user adds the extra row on the client using jscript and DOM.

I would like to avoid having to take another round trip to the server for adding an extra row.

I guess that's what you mean i have to do in order for zend to know that an extra row has been added.

kim
prodigitalson

Re: adding elemets to zend_form dynamically

Reply Threaded More More options
Print post
Permalink
In reply to this post by KimG

KimG wrote:
The user adds the extra row on the client using jscript and DOM.

I would like to avoid having to take another round trip to the server for adding an extra row.
Youre taking another trip.  this is the process

1.) User visit page with form - in the controller/action the form is created dynamically, view is render with the form.

2) User adds some fields via javascript and enters other data

3) Form is posted to a controller/action - During this action the form is reconstructed usually using the exact same logic as step 1 so it can be validated and then used (typically persisted in some way).

So what im saying is if the user adds fields in step 2, then you have to account for those fields in step 3. The easiest way is actually to encapuslate all the form construction within a form class, that way you can easily add detetction of the added fields and add them to the subform.

prodigitalson

Re: adding elemets to zend_form dynamically

Reply Threaded More More options
Print post
Permalink
In reply to this post by KimG

KimG wrote:
The user adds the extra row on the client using jscript and DOM.

I would like to avoid having to take another round trip to the server for adding an extra row.
Youre  not taking another trip.  this is the process

1.) User visit page with form - in the controller/action the form is created dynamically, view is render with the form.

2) User adds some fields via javascript and enters other data

3) Form is posted to a controller/action - During this action the form is reconstructed usually using the exact same logic as step 1 so it can be validated and then used (typically persisted in some way).

So what im saying is if the user adds fields in step 2, then you have to account for those fields in step 3. The easiest way is actually to encapuslate all the form construction within a form class, that way you can easily add detetction of the added fields and add them to the subform.

KimG

Re: adding elemets to zend_form dynamically

Reply Threaded More More options
Print post
Permalink
Thanx for info. It sounds very simple when you describe it but truth is i don't know how to do it.

I've been reading the zend_form documentation and googling till my eyes bled, but i'm still unable to come up with a solution.

can you give me some example code or point me to some example describing how to do it?

i like the idea of extending the zend_form class and have tried it, but can't get it to work in this situation.

kim


scss

Re: adding elemets to zend_form dynamically

Reply Threaded More More options
Print post
Permalink
Hi,
You can take a look at
Dynamically Adding Elements to Zend_Form
http://www.jeremykendall.net/2009/01/19/dynamically-adding-elements-to-zend-form/

scs


On Tue, Nov 3, 2009 at 1:47 PM, KimG <[hidden email]> wrote:

>
> Thanx for info. It sounds very simple when you describe it but truth is i
> don't know how to do it.
>
> I've been reading the zend_form documentation and googling till my eyes
> bled, but i'm still unable to come up with a solution.
>
> can you give me some example code or point me to some example describing how
> to do it?
>
> i like the idea of extending the zend_form class and have tried it, but
> can't get it to work in this situation.
>
> kim
>
>
>
> --
> View this message in context: http://old.nabble.com/adding-elemets-to-zend_form-dynamically-tp26157848p26160074.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
>
Ant Cunningham

Re: adding elemets to zend_form dynamically

Reply Threaded More More options
Print post
Permalink
scs wrote:

> Dynamically Adding Elements to Zend_Form
> http://www.jeremykendall.net/2009/01/19/dynamically-adding-elements-to-zend-form/

I dont think id ajax the form element. Id ont see any reason to...
perhaps if i was adding an entire Subform or a complex filedset but i
think its a waste of a request to add single fields if the schema is simple.

Still thats a good outline of the overall implementation details.