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

Updated Form class, bindValues function to set to empty array if no … #216

Merged
merged 5 commits into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,9 @@ public function bindValues(array $values = [], array $validationGroup = null)

// If there is a base fieldset, only hydrate beginning from the base fieldset
if ($this->baseFieldset !== null) {
$data = $data[$this->baseFieldset->getName()];
$data = array_key_exists($this->baseFieldset->getName(), $data)
? $data[$this->baseFieldset->getName()]
: [];
$this->object = $this->baseFieldset->bindValues($data, $validationGroup[$this->baseFieldset->getName()]);
} else {
$this->object = parent::bindValues($data, $validationGroup);
Expand Down
33 changes: 31 additions & 2 deletions test/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
use Zend\Form\Factory;
use Zend\Form\Fieldset;
use Zend\Form\Form;
use Zend\Hydrator;
use Zend\Hydrator\ObjectProperty as ObjectPropertyHydrator;
use Zend\InputFilter\BaseInputFilter;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFilterFactory;
use Zend\Hydrator;
use Zend\InputFilter\InputFilter;
use ZendTest\Form\TestAsset\Entity;
use ZendTest\Form\TestAsset\HydratorAwareModel;

Expand Down Expand Up @@ -818,6 +818,35 @@ public function testBindValuesWithWrappingPopulatesBoundObject()
], $model->foobar);
}

public function testFormBaseFieldsetBindValuesWithoutInputs()
{
$baseFieldset = new Fieldset('base_fieldset');
$baseFieldset->setUseAsBaseFieldset(true);

$form = new Form('default_form');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An unit test should be reduced to a minimum. The name for the form is not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

$form->add($baseFieldset);
$form->setHydrator(new ObjectPropertyHydrator());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use $form->setHydrator(new Hydrator\ObjectProperty());

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both are imported, and there are examples of each in the test case. #224 resolves the consistency issues, so I'll leave as-is for now.


$baseFieldsetInputFilter = new InputFilter();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove all these lines related to the input-filters. (830 - 835)
The result is the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Did indeed not make a difference for patch.


$formInputFilter = new InputFilter();
$formInputFilter->add($baseFieldsetInputFilter, 'base_fieldset');

$form->setInputFilter($formInputFilter);

$model = new stdClass();
$form->bind($model);

$data = [
'submit' => 'save',
];
$form->setData($data);

$form->isValid(); // Calls ->bindValues after validation (line: 817)

$this->assertObjectNotHasAttribute('submit', $model);
}

public function testHasFactoryComposedByDefault()
{
$factory = $this->form->getFormFactory();
Expand Down