Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/LiveComponent/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
## 3.1

- Use `aria-busy` attribute during component re-render
- Fix `LiveCollectionTrait::addCollectionItem()` reusing an index previously
freed by `removeCollectionItem()`, which caused a newly added item to be
rebound to the removed entity when used with a Doctrine collection.

## 3.0.0

Expand Down
20 changes: 18 additions & 2 deletions src/LiveComponent/src/LiveCollectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveArg;
use Symfony\UX\LiveComponent\Attribute\LiveProp;

/**
* @author Gábor Egyed <gabor.egyed@gmail.com>
Expand All @@ -22,6 +23,18 @@ trait LiveCollectionTrait
{
use ComponentWithFormTrait;

/**
* Tracks indexes removed from each collection so that newly added items
* never reuse a previously used index, which would make the form re-bind
* to a stale entity from the initial collection.
*
* Keyed by the collection field name passed to the add/remove actions.
*
* @var array<string, list<int>>
*/
#[LiveProp]
public array $liveCollectionRemovedKeys = [];
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Amoifr Do you think this could be "private" instead of "public"? I can’t think of a case where it would be useful outside this context

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — I'd love to make it private, but Symfony's PropertyAccess (used by LiveComponentHydrator) can't read or write private properties without an accessor, so the dehydration/rehydration cycle would silently drop the tombstone. I verified it with a tiny repro:

class T {
    #[LiveProp] private array $privateArray = ['init' => 1];
    #[LiveProp] public  array $publicArray  = ['init' => 1];
}

$pa = PropertyAccess::createPropertyAccessor();
$pa->getValue(new T(), 'privateArray'); // ❌ "Can't get a way to read the property "privateArray"…"
$pa->getValue(new T(), 'publicArray');  // ✅

Every existing #[LiveProp] in the codebase (ComponentWithFormTrait::$formName, $formValues, $isValidated, $validatedFields, ValidatableComponentTrait::$isValidated, …) is public for the same reason. I'll keep public here to match that convention — let me know if you'd rather I add a private + getter/setter pair instead, happy to rework.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Amoifr Thanks a lot for the explanation, I didn’t dig deep enough to understand the full context !

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JacquesMougin you're welcome ! I get to much pain in the first hours of the live component with this kind of things to remember about that 😄


#[LiveAction]
public function addCollectionItem(PropertyAccessorInterface $propertyAccessor, #[LiveArg] string $name): void
{
Expand All @@ -33,7 +46,8 @@ public function addCollectionItem(PropertyAccessorInterface $propertyAccessor, #
$data = [];
}

$index = [] !== $data ? max(array_keys($data)) + 1 : 0;
$usedKeys = array_merge(array_keys($data), $this->liveCollectionRemovedKeys[$name] ?? []);
$index = [] !== $usedKeys ? max($usedKeys) + 1 : 0;
$propertyAccessor->setValue($this->formValues, $propertyPath."[$index]", []);
}

Expand All @@ -44,6 +58,8 @@ public function removeCollectionItem(PropertyAccessorInterface $propertyAccessor
$data = $propertyAccessor->getValue($this->formValues, $propertyPath);
unset($data[$index]);
$propertyAccessor->setValue($this->formValues, $propertyPath, $data);

$this->liveCollectionRemovedKeys[$name][] = $index;
}

private function fieldNameToPropertyPath(string $collectionFieldName, string $rootFormName): string
Expand All @@ -60,4 +76,4 @@ private function fieldNameToPropertyPath(string $collectionFieldName, string $ro

return $propertyPath;
}
}
}
25 changes: 25 additions & 0 deletions src/LiveComponent/tests/Unit/LiveCollectionTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,31 @@ public function testRemoveCollectionItem(array $postedFormData, string $collecti
self::assertSame($expectedFormData[$component->formName], $component->formValues);
}

public function testAddCollectionItemDoesNotReusePreviouslyRemovedIndex()
{
$component = $this->createComponent([
'' => [
'items' => [
0 => [],
1 => [],
2 => [],
],
],
]);
$propertyAccessor = PropertyAccess::createPropertyAccessor();

$component->removeCollectionItem($propertyAccessor, 'items', 2);
$component->addCollectionItem($propertyAccessor, 'items');

self::assertSame([
'items' => [
0 => [],
1 => [],
3 => [],
],
], $component->formValues);
}

public static function provideAddedItems(): iterable
{
yield 'unnamed parent form' => [
Expand Down
Loading