Zend表单 - 如何设置子表单元素的值?

问题描述:

array 
'subQuantity' => 
array 
    'quantity_6' => string '23' (length=2) 
    'quantity_16' => string '23' (length=2) 
    'quantity_18' => string '23' (length=2) 
'update' => string 'Update' (length=6) 

美好的一天!我只是从现有的zend表单创建了一个子表单,并在表单提交时获取这些数据。根据发布的数据(quantity_ elements),我想将这些值设置为子表单元素。可能吗?提前致谢。 欢呼声和快乐编码!Zend表单 - 如何设置子表单元素的值?

不确定是要一次设置单个子窗体元素的值还是全部。尽管如此,你可以使用填充方法。例如:

$yourForm->populate(array(
    'subQuantity' => array(
     'quantity_6' => 'some value 1', 
     'quantity_16' => 'some value 2', 
     'quantity_18' => 'some value 3', 
    ) 
)); 

编辑:

下面是设置各个字段的几种方法:

$yourForm->populate(array(
     'subQuantity' => array(  
      'quantity_16' => 'some value', 
     ) 
)); 

// OR 

$yourForm->getSubForm('subQuantity')->getElement('quantity_16')->setValue('some value'); 

// this also should work (not sure if it works with underscore in 'quantity_16' though) 

$yourForm->subQuantity->quantity_16->setValue('some value'); 
+0

喜马辛,美好的一天!感谢你的及时回复。我只是想根据各个表单元素设置值。您发布的代码在其他时间可能会有很好的帮助。你能告诉我如何适当地做到这一点,这将是一个很大的帮助。感谢和更多的权力。 – 2011-04-05 05:09:47

+0

@Zend_Newbie_Dev。我添加了编辑我的答案,包括设置个人值。 – Marcin 2011-04-05 07:40:10

+1

@Zend_Newbie_Dev:但昨天你已经提出了完全相同的问题,并且昨天已经回答了(也由Marcin BTW)。为什么再问一次? – 2011-04-05 09:30:07

$formSuper = new ContractLink_Form_ContractAllotmentSuper(); 
    foreach($allotments as $key => $allotment) 
    { 
     $form = new ContractLink_Form_ContractAllotment(); 
     $form->populate($allotment); 
     $formSuper->addSubForm($form, 'contractAllotmentForm' . $key); 
    }