Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Collection attempts to access object property on $this->targetElement #6263

Closed
erispre opened this issue May 13, 2014 · 4 comments
Closed

Collection attempts to access object property on $this->targetElement #6263

erispre opened this issue May 13, 2014 · 4 comments
Assignees
Milestone

Comments

@erispre
Copy link

erispre commented May 13, 2014

This issue applies to Zend\Form\Element\Collection. When setting the target element of the collection, a Zend\Form\ElementInterface is expected (on Collection::setTargetElement()). This promotes the use of regular form elements, like Zend\Form\Element\Text to be used as a target element.

However, when using e.g. Text as a target element, binding an ArrayObject with a few values set and setting no hydrator, the Collection::extract() method attempts to access $this->targetElement->object, which is only defined if $this->targetElement is a Zend\Form\Fieldset. To clarify, consider the following test case:

$form = new \Zend\Form\Form('test');
$text = new \Zend\Form\Element\Text('text');
$form->add(array(
    'name' => 'text',
    'type' => 'Zend\Form\Element\Collection',
    'options' => array(
        'target_element' => $text, 'count' => 2,
    ),
));
$object = new \ArrayObject(array('text' => array('Foo', 'Bar')));
$form->bind($object);

This case will trigger a fatal error.

I suggest to add a check on whether $this->targetElement is a Fieldset and when everything fails just copying the value, assuming no hydration is needed. I am submitting a patch soon.

@erispre
Copy link
Author

erispre commented May 13, 2014

It has come to my attention that the same issue appears in one of the unit tests of Collection. These tests seem to call $targetElement->setObject(), which is not defined by Zend\Form\ElementInterface.

@adamlundrigan
Copy link
Contributor

Duplicate of #4482 ?

@SteveTalbot
Copy link

Related to #4482, but not a duplicate.

I've just created pull request #6518 containing our workaround for this.

adamlundrigan added a commit to adamlundrigan/zf2 that referenced this issue Nov 15, 2014
@Ocramius Ocramius added this to the 2.3.4 milestone Nov 19, 2014
@Ocramius Ocramius self-assigned this Nov 19, 2014
Ocramius pushed a commit that referenced this issue Nov 19, 2014
Ocramius added a commit that referenced this issue Nov 19, 2014
…nto develop

Close #6880
Close #6518
Close #6263
Forward port #6880
Forward port #6518
Forward port #6263
@Ocramius
Copy link
Member

Fixed via #6880, thanks!

Ocramius added a commit that referenced this issue Dec 28, 2014
Note that attribution will be given only as a comment to the commit, since rebasing/squashing
the commits to avoid changes to unaffected code-paths is problematic.

If this doesn't match expectations, then please ask for a revert and send the test code in a separate
pull request.
Ocramius added a commit that referenced this issue Dec 28, 2014
Ocramius added a commit that referenced this issue Dec 28, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants