Zf2 - 在集合内的元素中设置值 [英] Zf2 - Set value in element inside collection

查看:22
本文介绍了Zf2 - 在集合内的元素中设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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');

如何在集合我使用 zend Framework 2"的地址例如"字段中设置值.

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 ?!

表单代码为:

<?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 文件:

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

}

推荐答案

您需要将集合作为表单中的元素,并获得集合的字段集列表.在您看来:

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

这篇关于Zf2 - 在集合内的元素中设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆