我有一个嵌套的fieldset集合项目在另一个fieldset Ill,我试图设置元素变量。我可以成功地为Ill字段集设置元素,但无法在集合内生成或项字段集。
我有一个非常类似于这个例子的布局:https://framework.zend.com/manual/2.0/en/modules/zend.form.collections.html
嵌套表单的布局为:
ILLForm(形式)->生病(自定义字段)->项目(收集/自定义字段)
(i可以有多个Item字段集)
这是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 );
当我在视图控制器中查看结果时,没有项出现。以下是我试图绑定到字段集的数据示例:
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
它可能是一些简单的东西,我在构建数据的方式中忽略了它,但它逃避了我。
OK,因此似乎bind()函数不设置嵌套的字段集集合(但执行其他所有操作)。
还有一个函数没有这个问题,叫做setData()。我基本上改变了这部分代码:
$this->ILLForm->bind( $ill );
:
// 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);
}
根据代码中的注释,如果存在会话集,则通过setData()函数将数据推入字段(然后进行验证)。如果是新加载,则通过bind()函数将模型绑定到字段。