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

Commit

Permalink
fixed #102 do not ignore validation group for collections
Browse files Browse the repository at this point in the history
  • Loading branch information
svycka committed Jul 8, 2016
1 parent f2218e4 commit 8991ddb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/Element/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 8 additions & 2 deletions src/Fieldset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8991ddb

Please sign in to comment.