Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Live] Fixing bug with ComponentWithFormTrait and empty collections #857

Merged
merged 1 commit into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/LiveComponent/src/ComponentWithFormTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ private function extractFormValues(FormView $formView): array
// is already correct. For example, an expanded ChoiceType with
// options "text" and "phone" would already have a value in the format
// ["text"] (assuming "text" is checked and "phone" is not).
if (!($child->vars['expanded'] ?? false) && \count($child->children) > 0) {
if (!($child->vars['expanded'] ?? false) && ($child->vars['compound'] ?? false)) {
$values[$name] = $this->extractFormValues($child);

continue;
Expand Down
9 changes: 8 additions & 1 deletion src/LiveComponent/tests/Fixtures/Dto/BlogPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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

use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;

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

public $comments = [];
public $comments;

public function __construct()
{
// makes the class more complex & realistic - e.g. like an entity
$this->comments = new ArrayCollection();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ public function testItUsesKeysToRenderChildrenLiveIds(): void
->visit($urlSimple)
->assertSuccessful()
->assertHtml()
->dump()
->assertElementCount('ul li', 3)
// check for the live-id we expect based on the key
->assertContains('data-live-id="live-1745423312-the-key0"')
Expand Down
14 changes: 12 additions & 2 deletions src/LiveComponent/tests/Functional/Form/ComponentWithFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,22 @@ public function testFormValuesRebuildAfterFormChanges(): void
;

$div = $crawler->filter('[data-controller="live"]');
$liveProps = json_decode($div->attr('data-live-props-value'), true);
$dehydratedProps = json_decode($div->attr('data-live-props-value'), true);
// the embedded validated field should be gone, since its data is gone
$this->assertEquals(
['blog_post_form.content'],
$liveProps['validatedFields']
$dehydratedProps['validatedFields']
);

$browser
// empty the collection
->post('/_components/form_with_collection_type/removeComment', [
'body' => json_encode(['props' => $dehydratedProps, 'args' => ['index' => '1']]),
'headers' => ['X-CSRF-TOKEN' => $token],
])
->assertStatus(422)
->assertNotContains('<textarea id="blog_post_form_comments_')
;
}

public function testFormRemembersValidationFromInitialForm(): void
Expand Down