Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
35 changes: 28 additions & 7 deletions src/Illuminate/Html/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Illuminate\Routing\UrlGenerator;
use Illuminate\Session\Store as Session;
use Illuminate\Support\Traits\MacroableTrait;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;

class FormBuilder {

Expand Down Expand Up @@ -897,18 +898,38 @@ public function getValueAttribute($name, $value = null)
* Get the model value that should be assigned to the field.
*
* @param string $name
* @return string
* @return string|null
*/
protected function getModelValueAttribute($name)
{
if (is_object($this->model))
{
return object_get($this->model, $this->transformKey($name));
}
elseif (is_array($this->model))
$segments = explode('.', $this->transformKey($name));
$data = $this->model;

foreach ($segments as $key)
{
return array_get($this->model, $this->transformKey($name));
if (is_array($data))
{
$data = array_key_exists($key, $data) ? $data[$key] : null;
}
elseif ($data instanceof EloquentCollection)
{
$data = $data->find($key);
}
elseif ($data instanceof \ArrayAccess)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't this be part of the is_array clause?

if (is_array($data) or $data instanceof \ArrayAccess)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, because then the EloquentCollection block isn't hit, and also array_key_exists doesn't work on an ArrayAccess object as far as I know.

Copy link
Contributor

Choose a reason for hiding this comment

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

Here's what I meant:

if ($data instanceof EloquentCollection)
{
    $data = $data->find($key);
}
elseif (is_array($data) or $data instanceof \ArrayAccess)
{
    $data = isset($data[$key]) ? $data[$key] : null;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

http://3v4l.org/WuPaO combined with array_key_exists being more accurate than isset() is the reasoning.

{
$data = $data->offsetExists($key) ? $data->offsetGet($key) : null;
}
elseif (is_object($data))
{
$data = isset($data->$key) ? $data->$key : null;
}
else
{
return null;
}
}

return $data;
}

/**
Expand Down
31 changes: 31 additions & 0 deletions tests/Html/FormBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,31 @@ public function testImageInput()
$this->assertEquals('<input src="'. $url .'" type="image">', $image);
}


public function testNestedObjectsAndArrays()
{
$obj = new \StdClass;
$obj->stuff = new \Illuminate\Support\Collection([5 => ['bar' => 'baz']]);
$model = ['foo' => $obj];
$this->formBuilder->setModel($model);
$input = $this->formBuilder->text('foo[stuff][5][bar]');
$this->assertContains('value="baz"', $input);
}


public function testEloquentRelationshipValues()
{
$model = new \StdClass;
$related = [new FormBuilderModelStub(['id' => 1, 'name' => 'foo']), new FormBuilderModelStub(['id' => 2, 'name' => 'bar'])];
$model->related = new \Illuminate\Database\Eloquent\Collection($related);
$this->formBuilder->setModel($model);
$this->assertNotContains('value=', $this->formBuilder->text('related[0][name]'));
$this->assertContains('value="foo"', $this->formBuilder->text('related[1][name]'));
$this->assertContains('value="bar"', $this->formBuilder->text('related[2][name]'));
$this->assertNotContains('value=', $this->formBuilder->text('related[3][name]'));
}


protected function setModel(array $data, $object = true)
{
if ($object) $data = new FormBuilderModelStub($data);
Expand Down Expand Up @@ -436,4 +461,10 @@ public function __isset($key)
{
return isset($this->data[$key]);
}


public function getKey()
{
return isset($this->data['id']) ? $this->data['id'] : null;
}
}