質問

I've got an issue with my fieldset in Zend Framework 2. The user is able to save his personal data over a form. If he already save this, they should be prefilled with the data from database. This worked fine as it was only a form, but I need the address data in a fieldset, so that I can use it at other parts of my program. Now the input fields stays empty.

At the beginning, I fill the personal data in a session. My data looks like this:

object(Application\Model\Product\PersonalData)#247 (3) { 
    ["tel":protected]=> string(0) "" 
    ["birthday":protected]=> string(10) "2013-01-01" 
    ["address":protected]=> object(Application\Model\Account\Addresses)#248 (15) {    
        ["firstname":protected]=> string(5) "Ernie" 
        ["surname":protected]=> string(6) "Muppet"    
        ...
    } 
} 

As you can see, the data is already bind to the given objects, PersonalData as main, and Addresses for the fieldset. This seems to work then. Then I put it in my form:

$oForm->setData($oForm->getHydrator()->extract($_SESSION->getPersonalData()));
return new ViewModel(array('form'=>$oForm));

The addressFieldset has a hydrator and a binding, which does work, because all objects are perfectly filled. The only problem is, that when I open the page, the input-fields are empty, only birthday and telephone are filled, which are directly on the form

My form implements the address-fieldset like this:

$addressFieldset = new AddressFieldset($lang);
$addressFieldset->setUseAsBaseFieldset(true);
$addressFieldset->setName('address');
$this->add($addressFieldset);

I think that it might be just a problem with the correct addressing of my fieldset, but I can't explain why it would be filled correctly after posting the data then. All I want is that he fill the setData in my Fieldset.

I hope you understand my question and could help me.
Thanks a lot,
Svenja

EDIT: I analysed it a bit more now, it's very strange and I don't know what went wrong. When I debug I can see that $fieldset->getValue() returns all data I need. So I thought that the binding might be wrong and I did this to debug it step by step:

$values = $this->form->get('address')->getValue();
$addressFieldset = $this->form->get('address');
$aValues = $addressFieldset->getHydrator()->extract($values);
$addressFieldset->bindValues($aValues);

I went to the Fieldset.php and bindValues does perfectly what it should (it is only a recapitulation anyway(?)), call the hydrator and fill my object. But if I show in the elements, all values are NULL. I already checked my elements twice. The only different between the model and the elements is a different order of declaration. I call the method setUseAsBaseField(true) in the fieldset and the form, too. I can't understand why the data is in values but not in elements.

役に立ちましたか?

解決 2

I finally found the solution! At first, this link helped me: Populating fieldsets I didn't integrate the fieldset Input-Filter in the Form Input Filter.
But that wasn't all I have to do. My Form-Datamodel looks like this:

interface IPersonalData
{
    public function getTel();
    public function getBirthday();
    public function getAddress();

    public function setTel($tel);
    public function setAddress(IAddresses $address);
    public function setBirthday($birthday);
}

This is only the interface, but Address is an object. And that is the problem. When the form tries to fill the fieldset, he only accept arrays. So I have to extract my object in the getter-method to an array. I don't think that this is very useful, because I normaly want to get my object with this method. So I write a method "getAddressAsArray()" which looks like this:

public function getAddressAsArray()
{
    $oAddressHydrator = new AddressHydrator();
    if(isset($this->address))
    {
        return $oAddressHydrator->extract($this->address);
    }
    return array();
}

The extract-method of my hydrator changed like this:

public function extract($object)
{
    if(!$object instanceof IPersonalData)
    {
        throw new \InvalidArgumentException('$object must be an instance of Application\Model\Product\IPersonalData');
    }
    return array(
        'telephone' => $object->getTel(), 
        'address' => $object->getAddressAsArray(),
        'birthday' => $object->getBirthday(),
    );
}

他のヒント

It's very strange, because I have something and it good work. Are you confident that the expression $_SESSION->getPersonalData() to return the desired result? You are using a very strange session. http://framework.zend.com/manual/2.1/en/modules/zend.session.container.html

To EDIT (I'm sorry my english):

  1. You can use different types of hydrator, for example ArraySerializable (by default, your entity must have getArrayCopy() and exchangeArray() methods, in your case) ArraySerializable is the hydrator by default.
  2. First you have to bind a form with entity $form->bind(new Entity()); The entity will bind to the base fieldset. If the base fieldset not specified, the entity will bind with the form, because the form inherits fieldset. If the entity is an aggregate, ie, its properties contain another objects, for each of these objects should be your fildset. In constructor this children fieldsets you should use $this->setObject(new MyChildrenEntity()); As a result, the entity properties will be extracted to the form elements.
  3. After that, you should only work with the form, but not with its elements or fieldsets.
  4. You can pass any data in the form, so form elements will get this values. $form->setData($this->getRequest()->getPost()); This method use internal populateValues() method. If the form has elements with an appropriate name, they will be assigned to these values​​. If the form has fieldsets, they will also be transferred to these values​​.
  5. As long as the form fails to validate, the entity of these values ​​will not be assigned. These values ​​are assigned to entities only in case of successful verification. IsValid () method uses the internal method bindValues ​​() if the validation was successful.
  6. After successful validation, you can get the entity using getData() method $entity = $form->getData();

P.S.: If you are doing a complex "haсk", do not be offended by this simple explanation.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top