问题
I have a collection used in form, i know that if i want to set a value in a normal element i use:
$form->get('submit')->setValue('Update');
How can i set a value in the field 'Address "For example"' in a collection '' "I use zend Framework 2".
$companies = $this->getCompaniesTable()->getCompanies($id);
$form = new CompaniesForm();
$form->bind($companies);
$form->get('submit')->setValue('Update');
$form->get('submit')->setValue('Update');
$form->get('address')->setValue('test address');
Last line of the prev. code doesn't work, what's wrong ?!
The form code is:
<?php
namespace Companies\Form;
//use Zend\Form\Element;
use Zend\Form\Form;
class CompaniesForm extends Form {
public function __construct($name = null) {
parent::__construct('companies');
$this->setAttribute('method', 'post');
$this->setAttribute('enctype', 'multipart/form-data');
$this->add(array(
'name' => 'id',
'type' => 'Hidden'
));
$this->add(array(
'name' => 'name',
'type' => 'Text'
));
// address field
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'address',
'options' => array(
'count' => 1,
'should_create_template' => false,
'allow_add' => true,
'template_placeholder' => '__placeholder__',
'target_element' => array(
'type' => 'Companies\Form\AddressFieldset'
)
),
));
// address field
// email field
$this->add(array(
'name' => 'email',
'type' => 'text',
'options' => array('label' => 'Email:'),
));
$this->add(array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'value' => 'Go',
'id' => 'submitbutton'
)
));
}
}
The addressFieldset file:
<?php
namespace Companies\Form;
use Companies\Entity\Address;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;
class AddressField {
/**
* @var string
\ */
protected $name;
/**
* @param string $name
* @return Address
\ */
public function setName($name) {
$this->name = $name;
return $this;
}
/**
* @return string
\ */
public function getName() {
return $this->name;
}
}
class AddressFieldset extends Fieldset implements InputFilterProviderInterface {
public function __construct() {
parent::__construct('Address');
$this->setHydrator(new ClassMethodsHydrator(false))->setObject(new AddressField());
$this->add(array(
'name' => 'name',
'options' => array(
'label' => 'Address: '
)
));
}
/**
* @return array
\ */
public function getInputFilterSpecification() {
return array(
'name' => array(
//'required' => true,
)
);
}
}
回答1:
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');
}
回答2:
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.
回答3:
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.
回答4:
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);
来源:https://stackoverflow.com/questions/20217158/zf2-set-value-in-element-inside-collection