Zf2 - Set value in element inside collection

心不动则不痛 提交于 2019-12-06 14:43:40

You need to take collection as element from your form and you get list of field sets of your collection. In you view:

$collection = $form->get('address');
$fieldSets = $collection->getFieldsets();

// In your example you use one element as field set count = 1
// I guess you want to change field named address in your collection of the same name

$address = $fieldSets[0]->get('address');
$address->setValue('test adress');

//If you have more field sets in your collection for example count = 3 and you want this    
//value for all of them just iterate your field sets.

foreach($fieldsets as $fieldset){
    $fieldset->get('address')->setValue('test adress');
}

You can use Form populateValues() instead of setValue() method to do this: http://framework.zend.com/apidoc/2.3/classes/Zend.Form.Form.html#populateValues

So in Your case you should put in your controller:

$form = new CompaniesForm();
$addresses = array(
    array(
        'name' => 'address field 1 name'
    ),
    array(
        'name' => 'address field 2 name'
    ),
);
$form->get('address')->populateValues($addresses);

You can generate the addresses array using data from your DB for example.

Okay, it appears that some things are getting mixed up here. You try to manually assign Field-Values inside an EditForm. That's ... no good.

Imagine a simple Form

UserForm
   textInput ("name")
   textInput ("surname")
   numberInput ("age")

Now you want to edit a User. So you grab the Data from DB

//$userData = $db->get('userdata')...
$userData = array(
    'name'    => 'Peter',
    'surname' => 'Parker',
    'age'     => 23
);

To put the existing values into your form, all you have to do is to set the FORM into this data.

$form->setData($userData);

And that's all. In your case, obviously the data-structure is a little more different and more difficult. You'd have to have either a main Object that you could $form->bind() or your array that you set the forms data to using $form->setData() needs to be modified. In your case this would be:

$data = array(
    'id' => 1, // your objects id
    'name' => 'someName',
    'email' => 'foo@bar.baz',
    'address' => array(
        0 => array(
            'streetName'   => 'FooStreet',
            'streetNumber' => 42
        ),
        1 => array(
            'streetName'   => 'OofStreet',
            'streetNumber' => 24
        ),
    )
)

When you do $form->setData($data) using the above case, your form will be pre-filled with the data coming from the array. Naturally you'd have to get the data from the DB and not write an array manually.

If you wanted to do this in a controller using getTargetElement() will return the element or fieldset assigned in the collection.

$fieldset = $form->get('parent_fieldset');
$collection = $fieldset->get('collection');
$collectionFieldset = $collection->getTargetElement();
$collectionFieldset->get('element')->setValue($value);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!