I have a nested fieldset collection item inside another fieldset Ill that I am trying to set element variables for. I can successfully set elements for the Ill fieldset but am unable to generate or item fieldsets within the collection.
I have a very similar layout to this example: https://framework.zend.com/manual/2.0/en/modules/zend.form.collections.html
The layout of nested forms is:
ILLForm(Form)->Ill(FieldSet)->Item(Collection/Fieldsets)
(the Ill can have many Item fieldsets)
Here is the Indexcontroller:
$ill = new Ill('ill', $this->ILLCategories, $this->campuses, $this->getFromOptions);
$ill->setName($session->name);
$ill->setEmail($session->email);
$item_array = array();
if ( isset($session->department))
{
$ill->setDepartment($session->department);
$ill->setDivision($session->division);
if ( isset($session->formData->items))
{
$item_iterator = $session->formData->items;
$i = -1;
foreach ( $item_iterator as $item)
{
$i++;
$item_fieldset = new Item('Item '.($i+1), $this->ILLCategories, $this->getFromOptions);
$item_fieldset->setILLType($item->ILLType);
$item_fieldset->setGetFrom($item->getFrom);
$item_array[] = $item_fieldset;
}
}
else
{
$item_fieldset = new Item('Item 1', $this->ILLCategories, $this->getFromOptions);
$item_array[] = $item_fieldset;
}
}
else
{
$item_fieldset = new Item('Item 1', $this->ILLCategories, $this->getFromOptions);
$item_array[] = $item_fieldset;
}
$ill->items = $item_array;
$this->ILLForm->bind( $ill );
When I view the result in the view controller no items appear. Here is an example of the data I am trying to bind to the fieldsets:
object(ILL\Entity\Ill)[452]
protected 'name' => string 'Test' (length=4)
protected 'email' => string 'yay@yay.com' (length=11)
protected 'division' => string 'Test' (length=4)
protected 'department' => string 'Test' (length=4)
protected 'contact' => null
protected 'phone' => null
protected 'idNumber' => null
protected 'campus' => null
public 'item' =>
array (size=1)
0 =>
object(ILL\Entity\Item)[453]
private 'ILLType' => null
private 'requiredBy' => null
private 'urgent' => null
private 'citation' => null
private 'copyright' => null
private 'getFrom' => null
It could be something simple that I have overlooked in the way I have structured the data but it eludes me.
OK so it seems the bind() function does not set the nested fieldset collections (but does everything else).
There is another function that does not have this issue called setData(). I basically changed this part of the code:
$this->ILLForm->bind( $ill );
to this:
// If previously submitted then run the validation to generate messages, otherwise bind autogenerated data to form (username etc...)
if ( isset($session->formData))
{
$this->ILLForm->setData($session->formData);
$this->ILLForm->isValid($ill);
}
else
{
$this->ILLForm->bind($ill);
}
As per the comment in the code, if there is a session set then the data is pushed into the fields via the setData() function (and then validated). If its a fresh load then the model is bound to the fields via the bind() function.