Getting data for a form from a relation (#472)

This commit is contained in:
Vendin authored and Tom Shafer committed 2018-02-05 07:58:09 -07:00
1 parent cecb65626c
commit e1318fcfdd
2 files changed
+15 -2

No files matched your search

+2 -2
View File
@@ -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);
+13
View File
@@ -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;
}
}