Skip to content

Commit

Permalink
Zend_Form::setDefaults() check whether an array or Traversable object…
Browse files Browse the repository at this point in the history
… is passed
  • Loading branch information
mhujer committed Jun 3, 2014
1 parent 244e3d3 commit 89e2c5d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
7 changes: 6 additions & 1 deletion library/Zend/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -1268,11 +1268,16 @@ public function clearElements()
*
* Sets values for all elements specified in the array of $defaults.
*
* @param array $defaults
* @param array|Traversable $defaults
* @throws Zend_Form_Exception When invalid type is passed
* @return Zend_Form
*/
public function setDefaults($defaults)
{
if (!is_array($defaults) && !$defaults instanceof Traversable) {
throw new Zend_Form_Exception('Argument passed to setDefaults() must be of type array or Traversable.');
}

$eBelongTo = null;

if ($this->isArray()) {
Expand Down
26 changes: 26 additions & 0 deletions tests/Zend/Form/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4865,6 +4865,32 @@ public function testSetDefaultsAllowOverridingWithNonArrayParameter()
//this would throw a strict warning if the setDefaults() method requires param to be array
$form = new Zend_Form_FormTest_SetDefaults();
}

public function testCanSetElementDefaultValuesFromTraversable()
{
$this->testCanAddAndRetrieveMultipleElements();
$values = array(
'foo' => 'foovalue',
'bar' => 'barvalue',
'baz' => 'bazvalue',
'bat' => 'batvalue',
);
$traversable = new ArrayIterator($values);
$this->form->setDefaults($traversable);
$elements = $this->form->getElements();
foreach (array_keys($values) as $name) {
$this->assertEquals($name . 'value', $elements[$name]->getValue());
}
}

/**
* @expectedException Zend_Form_Exception
* @expectedExceptionMessage Argument passed to setDefaults() must be of type array or Traversable.
*/
public function testSetDefaultsWithInvalidTypeThrowsException()
{
$this->form->setDefaults(new stdClass());
}
}

class Zend_Form_FormTest_SetDefaults extends Zend_Form
Expand Down

0 comments on commit 89e2c5d

Please sign in to comment.