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

时间:2011-04-05 01:20:46

标签: php zend-framework zend-form

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),我想将值设置为子表单元素。可能吗?提前致谢。 欢呼和快乐的编码!

2 个答案:

答案 0 :(得分:8)

不确定是要一次设置单个子表单元素的值还是所有子表单元素的值。不过你可以使用populate方法。例如:

 $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');

答案 1 :(得分:0)

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