2013-11-26 14 views
1

我在形式使用的集合,我知道,如果我想在一個正常的元素設置值我使用: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, 
      ) 
     ); 
    } 

} 
+1

如果您沒有證明您的表單代碼,則無法回答。 – Sam

+0

我添加了一個表格代碼在後上方。 –

+0

而究竟你想改變什麼?;)其中一個Phone-Fieldsets的某個值?有點重要^^ – Sam

回答

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'); 
} 
+0

沒有在表單中找到[address]的名稱的元素,錯誤行: $ address = $ fieldSets [0] - > get('address'); –

+0

你的名字「地址」集合中有元素嗎?請向我們展示公司\表格\ AddressFieldset – kilop

+0

我現在在原帖中添加'地址Fieldset文件' –

2

好吧,看起來有些事情在這裏混淆了。您嘗試在EditForm內手動分配字段值。這是......不好。

想象一下,一個簡單的形式

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

現在你要編輯User。所以,如果你DB

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

獲取數據爲了把現有的值到表單中,所有你需要做的是形式設置成這個數據。

$form->setData($userData); 

而就是這樣。就你而言,顯然數據結構有點不同,也更困難。你必須要麼,你可以,你設定的表格數據使用$form->setData()需求$form->bind()或者您的數組修改主要對象。在你的情況,這將是:

$data = array(
    'id' => 1, // your objects id 
    'name' => 'someName', 
    'email' => '[email protected]', 
    'address' => array(
     0 => array(
      'streetName' => 'FooStreet', 
      'streetNumber' => 42 
     ), 
     1 => array(
      'streetName' => 'OofStreet', 
      'streetNumber' => 24 
     ), 
    ) 
) 

當使用上述情況做$form->setData($data),您的表格將被預填充從陣列中的數據來。當然,你必須從數據庫中獲取數據,而不是手動編寫數組。

+0

我使用$ form-> bind($ companies);在其他模塊中,當沒有收集和沒有多個表,但我不知道如何在這裏使用它,是否有一個完整的示例教程這種情況? –

+0

您的公司對象需要有一個'getAddress()'函數來返回AddressFieldset所需的對象數組。 – Sam

+0

我寫這個 'code' foreach($ addresses as $ address){ $ companies-> address = $ address; } 'code' $ form-> bind($ companies);這是對公司的補充,但沒有閱讀它。 –

3

您可以使用表格populateValues()代替的setValue()方法來做到這一點:http://framework.zend.com/apidoc/2.3/classes/Zend.Form.Form.html#populateValues

所以你的情況,你應該把你的控制器:

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

您可以生成地址陣列例如使用數據庫中的數據。

2

如果你想使用getTargetElement在控制器來完成()將返回元素或集合中的字段集分配。

$fieldset = $form->get('parent_fieldset'); 
$collection = $fieldset->get('collection'); 
$collectionFieldset = $collection->getTargetElement(); 
$collectionFieldset->get('element')->setValue($value);