我在形式使用的集合,我知道,如果我想在一個正常的元素設置值我使用:ZF2 - 裏面集合中元素設定值
$form->get('submit')->setValue('Update');
我怎麼能在設定的值字段'Address'例如''在一個集合''「我使用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');
上一行。代碼不起作用,怎麼了?!
形式的代碼是:
<?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'
)
));
}
}
的addressFieldset文件:
<?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,
)
);
}
}
如果您沒有證明您的表單代碼,則無法回答。 – Sam
我添加了一個表格代碼在後上方。 –
而究竟你想改變什麼?;)其中一個Phone-Fieldsets的某個值?有點重要^^ – Sam