Skip to content

Commit 8f5c600

Browse files
committed
bug #857 [Live] Fixing bug with ComponentWithFormTrait and empty collections (weaverryan)
This PR was merged into the 2.x branch. Discussion ---------- [Live] Fixing bug with ComponentWithFormTrait and empty collections | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Tickets | Fix #568 | License | MIT Empty collection objects - e.g. ArrayCollection - would not recursively tranform from their object to the underlying array if the collection was empty. I think this will fix the old #568 /cc `@jcrombez`. Cheers! Commits ------- 2dbade4 [Live] Fixing bug with ComponentWithFormTrait and empty collections
2 parents 9fc90d4 + 2dbade4 commit 8f5c600

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

src/LiveComponent/src/ComponentWithFormTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ private function extractFormValues(FormView $formView): array
235235
// is already correct. For example, an expanded ChoiceType with
236236
// options "text" and "phone" would already have a value in the format
237237
// ["text"] (assuming "text" is checked and "phone" is not).
238-
if (!($child->vars['expanded'] ?? false) && \count($child->children) > 0) {
238+
if (!($child->vars['expanded'] ?? false) && ($child->vars['compound'] ?? false)) {
239239
$values[$name] = $this->extractFormValues($child);
240240

241241
continue;

src/LiveComponent/tests/Fixtures/Dto/BlogPost.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\UX\LiveComponent\Tests\Fixtures\Dto;
1313

14+
use Doctrine\Common\Collections\ArrayCollection;
1415
use Symfony\Component\Validator\Constraints\Length;
1516
use Symfony\Component\Validator\Constraints\NotBlank;
1617

@@ -22,5 +23,11 @@ class BlogPost
2223
#[Length(min: 100, minMessage: 'The content field is too short')]
2324
public $content;
2425

25-
public $comments = [];
26+
public $comments;
27+
28+
public function __construct()
29+
{
30+
// makes the class more complex & realistic - e.g. like an entity
31+
$this->comments = new ArrayCollection();
32+
}
2633
}

src/LiveComponent/tests/Functional/EventListener/InterceptChildComponentRenderSubscriberTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ public function testItUsesKeysToRenderChildrenLiveIds(): void
120120
->visit($urlSimple)
121121
->assertSuccessful()
122122
->assertHtml()
123-
->dump()
124123
->assertElementCount('ul li', 3)
125124
// check for the live-id we expect based on the key
126125
->assertContains('data-live-id="live-1745423312-the-key0"')

src/LiveComponent/tests/Functional/Form/ComponentWithFormTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,22 @@ public function testFormValuesRebuildAfterFormChanges(): void
101101
;
102102

103103
$div = $crawler->filter('[data-controller="live"]');
104-
$liveProps = json_decode($div->attr('data-live-props-value'), true);
104+
$dehydratedProps = json_decode($div->attr('data-live-props-value'), true);
105105
// the embedded validated field should be gone, since its data is gone
106106
$this->assertEquals(
107107
['blog_post_form.content'],
108-
$liveProps['validatedFields']
108+
$dehydratedProps['validatedFields']
109109
);
110+
111+
$browser
112+
// empty the collection
113+
->post('/_components/form_with_collection_type/removeComment', [
114+
'body' => json_encode(['props' => $dehydratedProps, 'args' => ['index' => '1']]),
115+
'headers' => ['X-CSRF-TOKEN' => $token],
116+
])
117+
->assertStatus(422)
118+
->assertNotContains('<textarea id="blog_post_form_comments_')
119+
;
110120
}
111121

112122
public function testFormRemembersValidationFromInitialForm(): void

0 commit comments

Comments
 (0)