Skip to content

Commit 593040a

Browse files
SanderMullerSander Muller
andauthored
Fixes issue where the entity field was not set to the model value (#599)
Co-authored-by: Sander Muller <[email protected]>
1 parent d37564a commit 593040a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Kris/LaravelFormBuilder/Fields/FormField.php

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

144144
if (($value === null || $value instanceof \Closure) && !$isChild) {
145-
$attributeName = $this->getOption('property', $this->name);
145+
if ($this instanceof EntityType) {
146+
$attributeName = $this->name;
147+
} else {
148+
$attributeName = $this->getOption('property', $this->name);
149+
}
150+
146151
$this->setValue($this->getModelValueAttribute($this->parent->getModel(), $attributeName));
147152
} elseif (!$isChild) {
148153
$this->hasDefault = true;

tests/FormTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,26 @@ public function it_can_use_model_property_to_set_value()
794794
$this->assertEquals($form->alias_accessor->getValue(), $this->model->accessor);
795795
}
796796

797+
/** @test */
798+
public function it_sets_entity_field_value_to_the_entity_model_value()
799+
{
800+
$dummyModel = new DummyModel();
801+
$dummyModel->id = 1;
802+
803+
$this->model->dummy_model_id = $dummyModel->id;
804+
805+
$form = $this->formBuilder
806+
->plain([
807+
'model' => $this->model,
808+
])
809+
->add('dummy_model_id', 'entity', [
810+
'class' => DummyModel::class,
811+
'property' => 'name',
812+
]);
813+
814+
$this->assertEquals($form->dummy_model_id->getValue(), $this->model->dummy_model_id);
815+
}
816+
797817
/** @test */
798818
public function it_reads_configuration_properly()
799819
{

0 commit comments

Comments
 (0)