Skip to content
This repository has been archived by the owner on Feb 26, 2018. It is now read-only.

Commit

Permalink
Don't explode when asking for a value from a bound array that isn't set
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed Mar 25, 2016
1 parent 5fc21cb commit c6ccd41
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/AdamWathan/Form/Binding/BoundData.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ protected function dataGet($target, $keyParts, $default)

$key = array_shift($keyParts);

if (is_array($target) && isset($target[$key])) {
return $this->dataGet($target[$key], $keyParts, $default);
if (is_array($target)) {
return isset($target[$key]) ? $this->dataGet($target[$key], $keyParts, $default) : $default;
}

if (property_exists($target, $key) || method_exists($target, '__get')) {
Expand Down
27 changes: 27 additions & 0 deletions tests/BindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ public function testBindArray()
$this->assertEquals($expected, $result);
}

public function testBindArrayWithMissingKey()
{
$array = ['first_name' => 'John'];
$this->form->bind($array);

$expected = '<input type="text" name="last_name">';
$result = (string) $this->form->text('last_name');
$this->assertEquals($expected, $result);
}

public function testBindNestedArray()
{
$array = [
Expand All @@ -178,6 +188,23 @@ public function testBindNestedArray()
$this->assertEquals($expected, $result);
}

public function testBindNestedArrayWithMissingKey()
{
$array = [
'address' => [
'tree' => [
'nested' => 'Bird'
],
],
];

$this->form->bind($array);

$expected = '<input type="text" name="address[notSet]">';
$result = (string) $this->form->text('address[notSet]');
$this->assertEquals($expected, $result);
}

public function testBindNestedObject()
{
$object = json_decode(json_encode([
Expand Down

0 comments on commit c6ccd41

Please sign in to comment.