Zend Form - How to set values on sub form elements?

青春壹個敷衍的年華 提交于 2019-12-21 21:10:44

问题


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)

Good day! I just created a subform from my existing zend form and procures this data when form submits. Based on posted data (the quantity_ elements), I would like to set the values to subform elements. Is it possible? Thanks in advance. cheers and happy coding!


回答1:


Not sure whether you want to set values of individual subform elements or all of them at once. Nevertheless you can use populate method. For example:

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

EDIT:

Here are few ways of setting individual fields:

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



回答2:


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


来源:https://stackoverflow.com/questions/5546125/zend-form-how-to-set-values-on-sub-form-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!