Skip to content

Commit

Permalink
Merge pull request #17 from ARCANEDEV/feature-mutate_related_models
Browse files Browse the repository at this point in the history
[5.4] Mutating form attributes in related models
  • Loading branch information
arcanedev-maroc authored Feb 6, 2018
2 parents b10aacd + fa64521 commit 3193f48
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
23 changes: 18 additions & 5 deletions src/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,30 @@ public function getValueAttribute($name, $value = null)
/**
* Get the model value that should be assigned to the field.
*
* @param string $name
* @param string $name
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return mixed
*/
private function getModelValueAttribute($name)
private function getModelValueAttribute($name, $model = null)
{
if (is_null($model)) {
$model = $this->model;
}

$key = $this->transformKey($name);

return method_exists($this->model, 'getFormValue')
? $this->model->getFormValue($key)
: data_get($this->model, $key);
if (strpos($key, '.') !== false) {
$keys = explode('.', $key, 2);

return $this->getModelValueAttribute($keys[1],
$this->getModelValueAttribute($keys[0], $model)
);
}

return method_exists($model, 'getFormValue')
? $model->getFormValue($key)
: data_get($model, $key);
}

/**
Expand Down
31 changes: 24 additions & 7 deletions tests/Traits/FormAccessible.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
*/
class FormAccessible extends TestCase
{
/* ------------------------------------------------------------------------------------------------
/* -----------------------------------------------------------------
| Properties
| ------------------------------------------------------------------------------------------------
| -----------------------------------------------------------------
*/

/**
* The model data.
*
Expand All @@ -40,10 +41,11 @@ class FormAccessible extends TestCase
*/
protected $form;

/* ------------------------------------------------------------------------------------------------
/* -----------------------------------------------------------------
| Main Functions
| ------------------------------------------------------------------------------------------------
| -----------------------------------------------------------------
*/

public function setUp()
{
parent::setUp();
Expand All @@ -70,10 +72,11 @@ public function setUp()
$this->form = new FormBuilder($this->html, $this->urlGenerator, $session);
}

/* ------------------------------------------------------------------------------------------------
| Test Functions
| ------------------------------------------------------------------------------------------------
/* -----------------------------------------------------------------
| Tests
| -----------------------------------------------------------------
*/

/** @test */
public function it_can_mutate_values_for_forms()
{
Expand Down Expand Up @@ -112,4 +115,18 @@ public function it_can_get_related_value_for_forms()

$this->assertSame('abcde st', $model->getFormValue('address.street'));
}

/** @test */
public function it_can_mutate_related_values_for_forms()
{
$model = new ModelThatUsesForms($this->modelData);
$model->setRelation(
'related',
new ModelThatUsesForms($this->modelData)
);
$this->form->setModel($model);

$this->assertSame($this->form->getValueAttribute('related[string]'), 'ponmlkjihgfedcba');
$this->assertSame($this->form->getValueAttribute('related[created_at]'), $this->now->timestamp);
}
}

0 comments on commit 3193f48

Please sign in to comment.