From a3d42da280c6927bc6df7b04ce3f9fdd5a05e461 Mon Sep 17 00:00:00 2001 From: Adrian Gunawan Date: Sat, 14 May 2016 20:50:52 +1000 Subject: [PATCH 1/2] Transform the field name when getting the form value from model --- src/FormBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index a6ebd53..52390a7 100755 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -1103,7 +1103,7 @@ class FormBuilder protected function getModelValueAttribute($name) { if (method_exists($this->model, 'getFormValue')) { - return $this->model->getFormValue($name); + return $this->model->getFormValue($this->transformKey($name)); } return data_get($this->model, $this->transformKey($name)); From 0625f71fd937572cf7c49bf5f05e883224803410 Mon Sep 17 00:00:00 2001 From: Adrian Gunawan Date: Thu, 19 May 2016 14:39:49 +1000 Subject: [PATCH 2/2] Fix the issue of getting form value from related model --- src/Eloquent/FormAccessible.php | 4 ++++ tests/FormAccessibleTest.php | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/src/Eloquent/FormAccessible.php b/src/Eloquent/FormAccessible.php index b54bffe..a930e51 100644 --- a/src/Eloquent/FormAccessible.php +++ b/src/Eloquent/FormAccessible.php @@ -39,6 +39,10 @@ trait FormAccessible // retrieval from the model to a form that is more useful for usage. if ($this->hasFormMutator($key)) { return $this->mutateFormAttribute($key, $value); + } else { + + // No form mutator, let the model resolve this + return data_get($this, $key); } return $value; diff --git a/tests/FormAccessibleTest.php b/tests/FormAccessibleTest.php index 77d9524..db71bed 100644 --- a/tests/FormAccessibleTest.php +++ b/tests/FormAccessibleTest.php @@ -25,6 +25,9 @@ class FormAccessibleTest extends PHPUnit_Framework_TestCase $this->modelData = [ 'string' => 'abcdefghijklmnop', 'email' => 'tj@tjshafer.com', + 'address' => [ + 'street' => 'abcde st' + ], 'created_at' => $this->now, 'updated_at' => $this->now, ]; @@ -44,6 +47,12 @@ class FormAccessibleTest extends PHPUnit_Framework_TestCase $this->assertEquals($model->getFormValue('created_at'), $this->now->timestamp); } + public function testItCanGetRelatedValueForForms() + { + $model = new ModelThatUsesForms($this->modelData); + $this->assertEquals($model->getFormValue('address.street'), 'abcde st'); + } + public function testItCanStillMutateValuesForViews() { $model = new ModelThatUsesForms($this->modelData);