How to populate zend form data of multi array input fields

469 Views Asked by At

Hi I have the following form in zend

<?php
/**
 * Admin/modules/admin/forms/TransportRoute.php
 * @uses TransportRoute Admission Form
 */

class Admin_Form_TransportRoute extends Zend_Form
{
    public function init()
    {
        $this->setMethod('post');

        $stopageDetailsForm = new Zend_Form_SubForm();
        $stopageDetailsForm->setElementsBelongTo('transport_route_stopage');

        $sd_stopage = $this->CreateElement('text','stopage')
                                ->setAttribs(array('placeholder'=>'Stopage Name', 'mendatory'=>'true'))
                                ->setRequired(true)
                                ->addFilter(new Zend_Filter_StringTrim())
                                ->setDecorators(array( array('ViewHelper') ))
                                ->setIsArray(true)
                                ->addValidators(array(
                                        array('NotEmpty', true, array('messages' => 'Please enter Stopage Name')),
                                        array('stringLength',true,array(1, 6, 'messages'=> 'Stopage Name must be 2 to 40 characters long.'))
        ));

        $sd_stopage_fee = $this->CreateElement('text','stopage_fee')
                                ->setAttribs(array('placeholder'=>'Route Fee', 'mendatory'=>'true'))
                                ->setRequired(true)
                                ->addFilter(new Zend_Filter_StringTrim())
                                ->setDecorators(array( array('ViewHelper') ))
                                ->setIsArray(true)
                                ->addValidators(array(
                                        array('NotEmpty', true, array('messages' => 'Please enter Stopage Fee')),
        ));
        $stopageDetailsForm->addElements ( array (
                $sd_stopage,
                $sd_stopage_fee,
        ) );
        $this->addSubForm($stopageDetailsForm, 'transport_route_stopage');

               //all sub form end here
        $id = $this->CreateElement('hidden','id')
                            ->setDecorators(array( array('ViewHelper') ));
        $this->addElement($id);
        $this->setDecorators(
                array(
                        'PrepareElements',
                        array('viewScript'))
        );
    }
}

This is absolutely working fine when I render this form as below:

<div class="row-fluid stopage_block">
            <div class="span5">
                <?php echo $stopageDetail->stopage;?>
            </div>
            <div class="span4">
                <?php echo $stopageDetail->stopage_fee;?>
            </div>
</div>

But at the time of adding a record, I make clones of the div of class "stopage_block" and save them in the database. Now all my concerns are how to populate all the values by using a foreach loop that were inserted through clones of the div.

I have the following arrays

array('stopage' => 'India','stopage_fee' => 5000);
array('stopage' => 'US','stopage_fee' => 50000);
array('stopage' => 'Nepal','stopage_fee' => 2000);

How to populate back these values in my current form by using any loop or something else.

Thanks.

1

There are 1 best solutions below

0
On

There is a method getSubForms in Zend_Form, you can use it.

Also, I would recommend you to take a look at the following article http://framework.zend.com/manual/1.11/en/zend.form.advanced.html. I guess it's exactly what you are looking for.