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

Jay*_*oso 2 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)
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*cin 8

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

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

编辑:

以下是设置单个字段的几种方法:

$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');
Run Code Online (Sandbox Code Playgroud)