Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Correctly hydrate when using base fieldset #2716

Closed
wants to merge 2 commits into from

2 participants

Michaël Gallego Matthew Weier O'Phinney
Michaël Gallego

This fix an odd bug that I discovered while writing my webinar code. I think nobody reported it because it only occurs when using base fieldset and ArraySerializable hydrator (most people use ClassMethods when the use case is more complex I suppose).

Basically, the problem is that even if a base fieldset was set, the form itself was hydrated. The logic was correctly done for extracting phase but not for hydrating phase.

Matthew Weier O'Phinney

Can you provide a test, please?

Matthew Weier O'Phinney weierophinney referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney [#2716] Fix form image captcha test
- tearDown() was referencing an invalid property, leading to errors.
6d31aa4
Matthew Weier O'Phinney weierophinney referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney Merge branch 'hotfix/2716' into develop
Forward port #2716
771b5e3
Deleted user Unknown referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney [#2716] Fix form image captcha test
- tearDown() was referencing an invalid property, leading to errors.
a435cf6
Deleted user Unknown referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney Merge branch 'hotfix/2716'
Close #2716
cf33738
Deleted user Unknown referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney Merge branch 'hotfix/2716' into develop
Forward port #2716
409cd41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 9, 2012
  1. Michaël Gallego
  2. Michaël Gallego

    Add test

    bakura10 authored
This page is out of date. Refresh to see the latest.
9 library/Zend/Form/Form.php
View
@@ -287,7 +287,14 @@ public function bindValues(array $values = array())
}
$data = $this->prepareBindData($data, $this->data);
- $this->object = parent::bindValues($data);
+
+ // If there is a base fieldset, only hydrate beginning from the base fieldset
+ if ($this->baseFieldset !== null) {
+ $data = $data[$this->baseFieldset->getName()];
+ $this->object = $this->baseFieldset->bindValues($data);
+ } else {
+ $this->object = parent::bindValues($data);
+ }
}
/**
38 tests/ZendTest/Form/FormTest.php
View
@@ -19,6 +19,7 @@
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFilterFactory;
use Zend\Stdlib\Hydrator;
+use ZendTest\Form\TestAsset\Entity;
class FormTest extends TestCase
{
@@ -1161,4 +1162,41 @@ public function testResetPasswordValueIfFormIsNotValid()
$this->assertEquals('', $this->form->get('password')->getValue());
}
+
+ public function testCorrectlyHydrateBaseFieldsetWhenHydratorThatDoesNotIgnoreInvalidDataIsUsed()
+ {
+ $fieldset = new Fieldset('example');
+ $fieldset->add(array(
+ 'name' => 'foo'
+ ));
+
+ // Add an hydrator that ignores if values does not exist in the
+ $fieldset->setObject(new Entity\SimplePublicProperty());
+ $fieldset->setHydrator(new \Zend\Stdlib\Hydrator\ObjectProperty());
+
+ $this->form->add($fieldset);
+ $this->form->setBaseFieldset($fieldset);
+ $this->form->setHydrator(new \Zend\Stdlib\Hydrator\ObjectProperty());
+
+ // Add some inputs that do not belong to the base fieldset
+ $this->form->add(array(
+ 'type' => 'Zend\Form\Element\Submit',
+ 'name' => 'submit'
+ ));
+
+ $object = new Entity\SimplePublicProperty();
+ $this->form->bind($object);
+
+ $this->form->setData(array(
+ 'submit' => 'Confirm',
+ 'example' => array(
+ 'foo' => 'value example'
+ )
+ ));
+
+ $this->assertTrue($this->form->isValid());
+
+ // Make sure the object was not hydrated at the "form level"
+ $this->assertFalse(isset($object->submit));
+ }
}
16 tests/ZendTest/Form/TestAsset/Entity/SimplePublicProperty.php
View
@@ -0,0 +1,16 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Form
+ */
+
+namespace ZendTest\Form\TestAsset\Entity;
+
+class SimplePublicProperty
+{
+ public $foo;
+}
Something went wrong with that request. Please try again.