From e3956c3b1678abdf9fac879452f6b6313a525b1d Mon Sep 17 00:00:00 2001 From: David Windell Date: Fri, 8 Jul 2016 16:04:57 +0100 Subject: [PATCH 1/2] Add failing test case --- test/FormTest.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/FormTest.php b/test/FormTest.php index f5a8ccca..884083d1 100644 --- a/test/FormTest.php +++ b/test/FormTest.php @@ -567,6 +567,35 @@ public function testSettingValidationGroupBindsOnlyThoseValuesToModel() $this->assertObjectNotHasAttribute('foobar', $model); } + public function testSettingValidationGroupWithoutCollectionBindsOnlyThoseValuesToModel() + { + $model = new stdClass; + $dataWithoutCollection = [ + 'foo' => 'abcde' + ]; + $this->populateForm(); + $this->form->add([ + 'type' => 'Zend\Form\Element\Collection', + 'name' => 'categories', + 'options' => [ + 'count' => 0, + 'target_element' => [ + 'type' => 'ZendTest\Form\TestAsset\CategoryFieldset' + ] + ] + ]); + $this->form->setHydrator(new Hydrator\ObjectProperty()); + $this->form->bind($model); + $this->form->setData($dataWithoutCollection); + $this->form->setValidationGroup(array('foo')); + $this->form->isValid(); + + $this->assertObjectHasAttribute('foo', $model); + $this->assertEquals('abcde', $model->foo); + $this->assertObjectNotHasAttribute('categories', $model); + $this->assertObjectNotHasAttribute('foobar', $model); + } + public function testCanBindModelsToArraySerializableObjects() { $model = new TestAsset\Model(); From 8991ddb1e9435a9a37240b138131bbf990951b9b Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Fri, 8 Jul 2016 19:03:51 +0300 Subject: [PATCH 2/2] fixed #102 do not ignore validation group for collections --- src/Element/Collection.php | 6 ++++-- src/Fieldset.php | 10 ++++++++-- src/Form.php | 5 +++-- test/FormTest.php | 2 +- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/Element/Collection.php b/src/Element/Collection.php index 2d58b67f..fbb793ed 100644 --- a/src/Element/Collection.php +++ b/src/Element/Collection.php @@ -261,16 +261,18 @@ public function allowValueBinding() * Bind values to the object * * @param array $values + * @param array $validationGroup + * * @return array|mixed|void */ - public function bindValues(array $values = []) + public function bindValues(array $values = [], array $validationGroup = null) { $collection = []; foreach ($values as $name => $value) { $element = $this->get($name); if ($element instanceof FieldsetInterface) { - $collection[] = $element->bindValues($value); + $collection[] = $element->bindValues($value, $validationGroup); } else { $collection[] = $value; } diff --git a/src/Fieldset.php b/src/Fieldset.php index 89086707..ea26ad5b 100644 --- a/src/Fieldset.php +++ b/src/Fieldset.php @@ -557,9 +557,11 @@ public function allowValueBinding() * Bind values to the bound object * * @param array $values + * @param array $validationGroup + * * @return mixed|void */ - public function bindValues(array $values = []) + public function bindValues(array $values = [], array $validationGroup = null) { $objectData = $this->extract(); $hydrator = $this->getHydrator(); @@ -568,6 +570,10 @@ public function bindValues(array $values = []) foreach ($this->iterator as $element) { $name = $element->getName(); + if ($validationGroup && (array_key_exists($name, $validationGroup) || !in_array($name, $validationGroup))) { + continue; + } + if (!array_key_exists($name, $values)) { if (!($element instanceof Collection)) { continue; @@ -579,7 +585,7 @@ public function bindValues(array $values = []) $value = $values[$name]; if ($element instanceof FieldsetInterface && $element->allowValueBinding()) { - $value = $element->bindValues($value); + $value = $element->bindValues($value, empty($validationGroup[$name]) ? null : $validationGroup[$name]); } // skip post values for disabled elements, get old value from object diff --git a/src/Form.php b/src/Form.php index 7cd274cc..87736634 100644 --- a/src/Form.php +++ b/src/Form.php @@ -357,13 +357,14 @@ public function bindValues(array $values = []) } $data = $this->prepareBindData($data, $this->data); + $validationGroup = $this->getValidationGroup(); // 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); + $this->object = $this->baseFieldset->bindValues($data, $validationGroup[$this->baseFieldset->getName()]); } else { - $this->object = parent::bindValues($data); + $this->object = parent::bindValues($data, $validationGroup); } } diff --git a/test/FormTest.php b/test/FormTest.php index 884083d1..7f2841d4 100644 --- a/test/FormTest.php +++ b/test/FormTest.php @@ -587,7 +587,7 @@ public function testSettingValidationGroupWithoutCollectionBindsOnlyThoseValuesT $this->form->setHydrator(new Hydrator\ObjectProperty()); $this->form->bind($model); $this->form->setData($dataWithoutCollection); - $this->form->setValidationGroup(array('foo')); + $this->form->setValidationGroup(['foo']); $this->form->isValid(); $this->assertObjectHasAttribute('foo', $model);