Skip to content

Commit

Permalink
Merge pull request #621 from rudiedirkx/collection-property-bug-620
Browse files Browse the repository at this point in the history
Fixes bug from #620 introduced in #598: general field option 'property' clashes with collection field
  • Loading branch information
rudiedirkx authored Sep 28, 2020
2 parents e1765d9 + 4215e6f commit 06b6773
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
Expand Down
2 changes: 1 addition & 1 deletion src/Kris/LaravelFormBuilder/Fields/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ protected function setupValue()
if ($this instanceof EntityType) {
$attributeName = $this->name;
} else {
$attributeName = $this->getOption('property', $this->name);
$attributeName = $this->getOption('value_property', $this->name);
}

$this->setValue($this->getModelValueAttribute($this->parent->getModel(), $attributeName));
Expand Down
40 changes: 40 additions & 0 deletions tests/Fields/CollectionTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,25 @@ public function it_creates_collection_with_child_form_with_correct_model()
}
}

/** @test */
public function it_creates_collection_with_child_form_with_correct_model_properties()
{
$items = new \Illuminate\Support\Collection([
(new DummyEloquentModel2())->forceFill(['id' => 1, 'foo' => 'bar']),
(new DummyEloquentModel2())->forceFill(['id' => 2, 'foo' => 'baz']),
]);

$model = (new DummyEloquentModel())->forceFill(['id' => 11]);
$model->setRelation('items', $items);

$form = $this->formBuilder->create('\LaravelFormBuilderCollectionTypeTest\Forms\NamespacedDummyFormCollectionForm', [
'model' => $model,
]);

$collectionValue = $form->getField('items')->getOption('data');
$this->assertEquals($items, $collectionValue);
}

/**
* @test
*/
Expand Down Expand Up @@ -227,4 +246,25 @@ class DummyEloquentModel2 extends Model {
class NamespacedDummyForm extends Form
{
}

class NamespacedDummyFormCollectionChildForm extends Form
{
function buildForm()
{
$this->add('foo', 'text');
}
}

class NamespacedDummyFormCollectionForm extends Form
{
function buildForm()
{
$this->add('items', 'collection', [
'type' => 'form',
'options' => [
'class' => NamespacedDummyFormCollectionChildForm::class,
],
]);
}
}
}
4 changes: 2 additions & 2 deletions tests/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ public function it_can_take_and_replace_existing_fields()
$this->plainForm->only('remember', 'name');

$this->assertEquals(2, count($this->plainForm->getFields()));

$this->assertTrue($this->plainForm->has('remember'));
$this->assertTrue($this->plainForm->has('name'));
$this->assertFalse($this->plainForm->has('description'));
Expand Down Expand Up @@ -788,7 +788,7 @@ public function it_can_use_model_property_to_set_value()
]);

$form->add('alias_accessor', 'choice', [
'property' => 'accessor',
'value_property' => 'accessor',
]);

$this->assertEquals($form->alias_accessor->getValue(), $this->model->accessor);
Expand Down

0 comments on commit 06b6773

Please sign in to comment.