Skip to content

Commit e237424

Browse files
authored
Merge pull request #598 from SanderMuller/allow-use-of-property-option-to-resolve-value
Allow use of the property option to resolve value
2 parents b6a4c4a + 65fd333 commit e237424

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

src/Kris/LaravelFormBuilder/Fields/FormField.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ protected function setupValue()
142142
}
143143

144144
if (($value === null || $value instanceof \Closure) && !$isChild) {
145-
$this->setValue($this->getModelValueAttribute($this->parent->getModel(), $this->name));
145+
$attributeName = $this->getOption('property', $this->name);
146+
$this->setValue($this->getModelValueAttribute($this->parent->getModel(), $attributeName));
146147
} elseif (!$isChild) {
147148
$this->hasDefault = true;
148149
}

tests/FormBuilderTestCase.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
class TestModel extends Model {
1313
protected $fillable = ['m', 'f'];
14+
15+
public function getAccessorAttribute($value)
16+
{
17+
return 'accessor value';
18+
}
1419
}
1520

1621
abstract class FormBuilderTestCase extends TestCase {

tests/FormTest.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -716,11 +716,12 @@ public function it_renders_rest_of_the_form_until_specified_field()
716716
/** @test */
717717
public function it_can_add_child_form_as_field()
718718
{
719-
$form = $this->formBuilder->plain();
719+
$model = ['song' => ['body' => 'test body'], 'title' => 'main title'];
720+
$form = $this->formBuilder->plain([
721+
'model' => $model,
722+
]);
720723
$customForm = $this->formBuilder->create('CustomDummyForm');
721724
$customForm->add('img', 'file')->add('name', 'text', ['label_show' => false]);
722-
$model = ['song' => ['body' => 'test body'], 'title' => 'main title'];
723-
$form->setModel($model);
724725

725726
$form
726727
->add('title', 'text', [
@@ -779,6 +780,20 @@ public function it_can_add_child_form_as_field()
779780
$this->fail('No exception on bad method call on child form.');
780781
}
781782

783+
/** @test */
784+
public function it_can_use_model_property_to_set_value()
785+
{
786+
$form = $this->formBuilder->plain([
787+
'model' => $this->model,
788+
]);
789+
790+
$form->add('alias_accessor', 'choice', [
791+
'property' => 'accessor',
792+
]);
793+
794+
$this->assertEquals($form->alias_accessor->getValue(), $this->model->accessor);
795+
}
796+
782797
/** @test */
783798
public function it_reads_configuration_properly()
784799
{
@@ -820,10 +835,11 @@ public function it_works_when_setModel_method_is_called()
820835
/** @test */
821836
public function it_removes_children_from_parent_type_fields()
822837
{
823-
$form = $this->formBuilder->plain();
824-
$customForm = $this->formBuilder->create('CustomDummyForm');
825838
$model = ['song' => ['title' => 'test song title', 'body' => 'test body'], 'title' => 'main title'];
826-
$form->setModel($model);
839+
$form = $this->formBuilder->plain([
840+
'model' => $model,
841+
]);
842+
$customForm = $this->formBuilder->create('CustomDummyForm');
827843

828844
$form
829845
->add('title', 'text')

0 commit comments

Comments
 (0)