Zf2 - Wert in element innerhalb der Sammlung

Habe ich eine Sammlung in form, ich weiß, dass wenn ich einen Wert in einem normalen element, die ich benutze:

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

Wie kann ich einen Wert in das Feld " Adresse "Zum Beispiel" "in einer Sammlung" "ich benutze 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');

Letzten Zeile des prev. code funktioniert nicht, was ist falsch ?!

Form-code:

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

}

Den addressFieldset Datei:

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

}
Nicht zu beantworten, ohne Sie beweisen Ihr Formular-code.
Ich habe ein Formular-code in den post oben.
Und was GENAU wollen Sie ändern? 😉 Ein bestimmter Wert auf einer der Telefon-Fieldsets? thi ist ein bisschen wichtiger ^^
Es ist AddressFieldset nicht TelephoneFieldset "meine hier falsch", Also will ich das ändern der Adressen-Felder.
Die Frage ist, welchen Zweck genau? Eine Sammlung ist eine Reihe von Fieldsets in diesem Fall. Also, wenn Sie wollen, um den Wert zu ändern müssen Sie ändern fieldset. Das ändern einer einzelnen Wert von EINEM der MEHRFACH gerenderten fieldsets ist ein bisschen schlecht (große design-Fehler).

InformationsquelleAutor ahmad soliman | 2013-11-26

Schreibe einen Kommentar