alab wrote:
> 
> Simon,
> would you give a tiny example to reproduce this and if it is
> reproduceable, i 
> will fix it asap.
> Greetings, Christian
> 

The below is a sub form being added inside my form class as I believe is
recommended practice. All it's doing is retrieving some items as an array.
Creating a quantity form, setting it to an array and naming it.

Then I loop through the items and add a text box to hold the quantity for
each element. The name being the id of the item in the database (so could be
any number in any order as they are not always sequential). I type the id it
as a string (although I believe from the docs this is no longer required).

I have not pasted the parts about setting form action, submit buttons etc.


class Purchasing_Form_Item_Management extends Zend_Form
{

public function init()
{
$items = $this->_getItems(); // Simply gets a multidimensional array from
somewhere!

$quantityForm = new Zend_SubForm();
$quantityForm->setIsArray(true);
$quantityForm->setName('quantity');

        // Loop through the orders
        foreach ($items AS $item) {

                // Add the quantity element
                $quantityForm->addElement('text', (string) $item->id, array(
                    'filters' => array('Float'),
                    'value' => $item['quantity'],
                    //'belongsto' => 'quantity', // - Makes no difference
whether it's on or off, as expected
                ));

        }

$this->addSubForm($quantityForm, 'quantity');
}
}


When checking the data it is as follows (this example is inside a controller
action for simplicity).


$data = $this->_request->getPost();

// Retrieve the form for validation
$form = new Purchasing_Form_Item_Management();

if (!$form->isValid($data)) {
      return false;
}
unset($data);

// Retrieve the values from the form
$values = $form->getValues();


$values will then be structured as so:

$values['quantity][0] = 10
$values['quantity][1] = 20
$values['quantity][2] = 30


If however you do away with the sub form and use the belongs to property of
the form element it works as expected and the array remains with the correct
index. This is fine however I need to use sub forms as I will also have
other fields I need indexed by the key id.

The code has been copied and pasted slightly restructured so any coding
errors are typing mistakes here as the code runs fine and works in 1.10.2.
Any obvious missing flags, or missing steps that are now required by array
notation forms are probably my fault!!

Thanks for your time.

Simon

-- 
View this message in context: 
http://zend-framework-community.634137.n4.nabble.com/Zend-Form-Array-Notation-values-being-re-indexed-tp2122097p2122625.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to