From e1318fcfdd1c713b7198557f1a62d33eb1da559b Mon Sep 17 00:00:00 2001 From: Vendin Date: Mon, 5 Feb 2018 17:58:09 +0300 Subject: [PATCH] Getting data for a form from a relation (#472) --- src/Eloquent/FormAccessible.php | 4 ++-- tests/FormAccessibleTest.php | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Eloquent/FormAccessible.php b/src/Eloquent/FormAccessible.php index 9b5d6d0..eb74013 100644 --- a/src/Eloquent/FormAccessible.php +++ b/src/Eloquent/FormAccessible.php @@ -49,7 +49,7 @@ trait FormAccessible unset($keys[0]); $key = implode('.', $keys); - if ($this->hasFormMutator($key)) { + if (method_exists($relatedModel, 'hasFormMutator') && $relatedModel->hasFormMutator($key)) { return $relatedModel->getFormValue($key); } @@ -81,7 +81,7 @@ trait FormAccessible * * @return bool */ - protected function hasFormMutator($key) + public function hasFormMutator($key) { $methods = $this->getReflection()->getMethods(ReflectionMethod::IS_PUBLIC); diff --git a/tests/FormAccessibleTest.php b/tests/FormAccessibleTest.php index 9f25686..2a1ef46 100644 --- a/tests/FormAccessibleTest.php +++ b/tests/FormAccessibleTest.php @@ -42,10 +42,13 @@ class FormAccessibleTest extends PHPUnit_Framework_TestCase public function testItCanMutateValuesForForms() { $model = new ModelThatUsesForms($this->modelData); + $user = new User(['name' => 'Anton']); + $model->setRelation('user', $user); $this->formBuilder->setModel($model); $this->assertEquals($model->getFormValue('string'), 'ponmlkjihgfedcba'); $this->assertEquals($model->getFormValue('created_at'), $this->now->timestamp); + $this->assertEquals('Get name for form: Anton', $model->getFormValue('user.name')); } public function testItCanMutateRelatedValuesForForms() @@ -159,3 +162,13 @@ class ModelThatDoesntUseForms extends Model return '1 second ago'; } } + +class User extends Model +{ + use FormAccessible; + + public function formNameAttribute($value) + { + return 'Get name for form: ' . $value; + } +}