Skip to content

Commit

Permalink
Merge pull request zendframework#4572 from joshribakoff/master
Browse files Browse the repository at this point in the history
Zend\Form Should throw exception if try to get() an element that does not exist
  • Loading branch information
weierophinney committed Jun 10, 2013
2 parents ec48e67 + 4ebdb06 commit 69a35c8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
6 changes: 4 additions & 2 deletions library/Zend/Form/Fieldset.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,16 @@ public function has($elementOrFieldset)
/**
* Retrieve a named element or fieldset
*
* @todo Should this raise an exception if no entry is found?
* @param string $elementOrFieldset
* @return ElementInterface
*/
public function get($elementOrFieldset)
{
if (!$this->has($elementOrFieldset)) {
return null;
throw new Exception\InvalidElementException(sprintf(
"No element by the name of [%s] found in form",
$elementOrFieldset
));
}
return $this->byName[$elementOrFieldset];
}
Expand Down
8 changes: 5 additions & 3 deletions tests/ZendTest/Form/FieldsetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,12 @@ public function testSetOptionsUseAsBaseFieldset()
$this->assertEquals('bar', $option);
}

public function testGetReturnsNull()
/**
* @expectedException Zend\Form\Exception\InvalidElementException
*/
public function testShouldThrowExceptionWhenGetInvalidElement()
{
$foo = $this->fieldset->get('foo');
$this->assertNull($foo);
$this->fieldset->get('doesnt_exist');
}

public function testBindValuesHasNoName()
Expand Down
8 changes: 8 additions & 0 deletions tests/ZendTest/Form/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ public function testCanComposeAnInputFilter()
$this->assertSame($filter, $this->form->getInputFilter());
}

/**
* @expectedException Zend\Form\Exception\InvalidElementException
*/
public function testShouldThrowExceptionWhenGetInvalidElement()
{
$this->form->get('doesnt_exist');
}

public function testDefaultNonRequiredInputFilterIsSet()
{
$this->form->add(new Element('foo'));
Expand Down

0 comments on commit 69a35c8

Please sign in to comment.