ahm*_*man 1 php zend-framework2
我有一个在表单中使用的集合,我知道,如果我想在普通元素中设置一个值,我使用:
$form->get('submit')->setValue('Update');
Run Code Online (Sandbox Code Playgroud)
如何在"地址"字段中设置值例如"'在集合中''"我使用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');
Run Code Online (Sandbox Code Playgroud)
最后一行的上一页.代码不起作用,有什么问题?!
表单代码是:
<?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'
)
));
}
}
Run Code Online (Sandbox Code Playgroud)
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,
)
);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
您需要从表单中将集合作为元素,并获得集合的字段集列表.在你看来:
$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');
}
Run Code Online (Sandbox Code Playgroud)