Zf2 - 在集合内部的元素中设置值

时间:2013-11-26 12:30:32

标签: php zend-framework2

我有一个在表单中使用的集合,我知道如果我想在普通元素中设置一个值,我会使用:

$form->get('submit')->setValue('Update');

如何在“地址”字段中设置值例如“'在集合中''”我使用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');

prev的最后一行。代码不起作用,出了什么问题?!

表单代码为:

<?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,
            )
        );
    }

}

4 个答案:

答案 0 :(得分: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');
}

答案 1 :(得分:3)

您可以使用Form 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 :(得分:2)

好吧,似乎有些事情在这里混淆了。您尝试在EditForm内手动分配字段值。那是......不好。

想象一个简单的表格

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

现在您要编辑User。所以你从数据库中获取数据

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

要将现有值放入表单,您只需将FORM设置为此数据。

$form->setData($userData);

就是这样。在您的情况下,显然数据结构更加不同,更加困难。您必须拥有一个可以$form->bind()的主对象,或者您需要修改使用$form->setData()设置表单数据的数组。在你的情况下,这将是:

$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
        ),
    )
)

当您使用上述情况$form->setData($data)时,您的表单将预先填充来自阵列的数据。当然,您必须从数据库获取数据,而不是手动编写数组。

答案 3 :(得分:2)

如果要在控制器中使用getTargetElement()执行此操作,将返回集合中指定的元素或字段集。

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