ZF2 Nested Collection元素没有任何价值

时间:2014-07-16 08:05:48

标签: php forms collections zend-framework2

我创建了一个带有嵌套Collection Customer(Form)的表单 - >类别(收集/字段集) - >标签(收集/字段集)。

这是: 一个(客户) - >许多(类别) 一个(Catogrie) - >许多(标签)

将客户绑定到表单后,看起来一切正常。 Hydrator获取对象和在Tags中创建的元素。

但在视图中,标签元素没有价值......

我已经检查了Hydrator的拼写错误,但一切都很好我复制/粘贴索引以确保。当我var_dump标签集合时,带有值的对象被绑定。

我真的不知道错误在哪里,这就是为什么我不在这里输入一些代码我觉得它会很多。如果您有任何想法,我可以向您显示您猜错误的代码。

问候。

铁夹。

编辑:

好的,我会尽力为您提供可读代码:)

class KontakteController extends AbstractActionController {



public function getKontaktAction()
{
    $formManager = $this->serviceLocator->get('FormElementManager');
    $kontaktForm = $formManager->get('KontakteManager\Form\KontakteForm');

    $id = $this->params()->fromRoute('id');
    $kontakt = $this->getKontakte()->getKontakt($id);
    if (!$id || !$kontakt) {
        return $this->redirect()->toRoute('kontakte', array(
            'action' => 'addKontakt'
        ));
    }

    $kontakt->initFirmaKommunikation($this->getKommunikation());
    $kontakt->initAdressen($this->getAdressen());
    $kontakt->initAnsprechpartner($this->getKontakte());
    $kontakt->initBankverbindungen($this->getBankverbindung());
    $kontakt->initFirmaKategorien($this->getKontakteKategorie());
    $kontakt->initPersonKategoiern($this->getKontakteKategorie());
    $kontaktForm->bind($kontakt);

    return new ViewModel(array(
        'kontaktForm'       => $kontaktForm,
        'geloescht'         => $kontakt->geloescht,
        'tags'              => $this->ladeTags(),
    ));
}

CustomerFieldset:

class KontakteForm extends Form implements InputFilterProviderInterface {


public function __construct()
{
    parent::__construct('kontakt');

    $this->setHydrator(new KontaktHydrator())
        ->setObject(new Kontakt());

    $this->add(array(
        'type' => 'Zend\Form\Element\Collection',
        'name' => 'firmaKategorien',
        'options' => array(
            'count' => 0,
            'allow_add'      => true,
            'allow_remove'   => true,
            'should_create_template' => false,
            'target_element' => array(
                'type' => 'KontakteManager\Form\KontaktKategorieFieldset'
            )
        )
    ));

}

/**
 * @return array
\*/
public function getInputFilterSpecification()
{
    return array();
}

CategorieFieldset:

class KontaktKategorieFieldset extends Fieldset {

public function __construct()
{
    parent::__construct('kontakteKategorie');
    $this->setHydrator(new KontaktKategorieFieldsetHydrator())
        ->setObject(new KontaktKategorie());

    $this->add(array(
        'name' => 'id',
        'type' => 'Zend\Form\Element\Hidden',
        'attributes' => array(
            'class' => 'form-control',
        ),
    ));

    $this->add(array(
        'name' => 'bezeichnung',
        'type' => 'Zend\Form\Element\Text',
        'attributes' => array(
            'class' => 'form-control',
        ),
        'options'   => array(
            'label' => 'Kategorie',
        ),
    ));

    $this->add(array(
        'type' => 'Zend\Form\Element\Collection',
        'name' => 'tags',
        'options' => array(
            'count' => 0,
            'allow_add'      => true,
            'allow_remove'   => true,
            'should_create_template' => false,
            'target_element' => array(
                'type' => 'KontakteManager\Form\TagFieldset'
            )
        )
    ));

} 

TagFielset:

class TagFieldset extends Fieldset implements InputFilterProviderInterface {

public function __construct()
{
    parent::__construct('Tag');
    $this->setHydrator(new TagHydrator())
        ->setObject(new Tag());

    $this->add(array(
        'name' => 'id',
        'type' => 'Zend\Form\Element\Hidden',
        'attributes' => array(
            'class' => 'form-control',
        ),
    ));

    $this->add(array(
        'name' => 'mehrsprachig',
        'type' => 'Zend\Form\Element\Hidden',
        'attributes' => array(
            'class' => 'form-control',
        ),
        'options'   => array(
            'label' => 'Mehrsprachig',
        ),
    ));

    $this->add(array(
        'name' => 'kategorieID',
        'type' => 'Zend\Form\Element\Hidden',
        'attributes' => array(
            'class' => 'form-control',
        ),
    ));

    $this->add(array(
        'name' => 'bezeichnung',
        'type' => 'Zend\Form\Element\Text',
        'attributes' => array(
            'class'     => 'form-control',
            'readonly'  => 'readonly',
        ),
        'options'   => array(
            'label' => 'Bezeichnung',
        ),
    ));
}


/**
 * @return array
\*/
public function getInputFilterSpecification()
{
    return array();
}

View代码如何尝试显示集合

                        <h5 class="text-primary"><strong>Kategorien</strong></h5>
                    <hr>
                    <?php foreach($kontaktForm->get('firmaKategorien') as $element): ?>
                    <div class="row">
                        <div class="col-lg-12">
                            <div class="col-lg-6">
                                <?php echo $this->formElement($element->get('bezeichnung')); ?>
                            </div>
                            <div class="col-lg-6">
                                <?php foreach($element->get('tags') as $tag): ?>
                                    <?php echo $this->formElement($tag->get('bezeichnung')); ?>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>

这里有一个示例结果:

<h5 class="text-primary"><strong>Kategorien</strong></h5>
<hr>
<div class="row">
<div class="col-lg-12">
    <div class="col-lg-6">
        <input type="text" name="firmaKategorien[0][bezeichnung]" class="form-control" value="Druckerei">
    </div>
    <div class="col-lg-6">
        <input type="text" name="firmaKategorien[0][tags][0][bezeichnung]" class="form-control" readonly="readonly" value="">
        <input type="text" name="firmaKategorien[0][tags][1][bezeichnung]" class="form-control" readonly="readonly" value="">
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

看看这是否会进行任何更改

$kontaktForm->bind($kontakt);
//Add this line
$kontaktForm->setData((Array)$kontakt);

请回复您收到的错误