Comments (4)
Edit: Example is now more dynamic.
For anyone facing the same issue, I've resolved it by creating a specific CustomLiveCollectionTrait for this usecase. This initializes the correct object.
Example code:
#[AsLiveComponent]
class VehicleFinancialFormComponent extends AbstractController
{
use DefaultActionTrait;
use CustomLiveCollectionTrait;
#[LiveProp]
public ?FinancialsModel $financials = null;
protected function instantiateForm(): FormInterface
{
.....
}
services:
Symfony\UX\LiveComponent\Metadata\LiveComponentMetadataFactory: '@ux.live_component.metadata_factory'
trait CustomLiveCollectionTrait
{
use LiveCollectionTrait;
public function __construct(
private readonly LiveComponentMetadataFactory $liveComponentMetadataFactory,
private readonly PropertyAccessorInterface $propertyAccessor
) {
}
#[LiveAction]
public function addCollectionItem(#[LiveArg] string $name): void
{
$propertyPath = $this->fieldNameToPropertyPath($name, $this->formName);
$value = $this->initializeValue($propertyPath);
$data = $this->propertyAccessor->getValue($this->formValues, $propertyPath);
if (!\is_array($data)) {
$this->propertyAccessor->setValue($this->formValues, $propertyPath, []);
$data = [];
}
$index = [] !== $data ? max(array_keys($data)) + 1 : 0;
$this->propertyAccessor->setValue($this->formValues, $propertyPath."[$index]", $value);
}
private function deHydrateClass(string $className): array
{
$propertyData = [];
$class = new $className();
foreach ((new PropertyInfoExtractor([new ReflectionExtractor()]))->getProperties($class::class) as $property) {
$propValue = $this->propertyAccessor->getValue($class, $property);
$propertyData[$property] = $this->deHydrateValue($propValue);
}
return $propertyData;
}
private function deHydrateValue(mixed $value): mixed
{
if ($value instanceof \DateTimeInterface) {
return $value->format(\DateTimeInterface::RFC3339);
}
if ($value instanceof \BackedEnum) {
return $value->value;
}
if (\is_bool($value) || null === $value || is_numeric($value) || \is_string($value)) {
return $value;
}
throw new \LogicException(sprintf('Unable to dehydrate value of type "%s".', get_debug_type($value)));
}
private function initializeValue(string $propertyPath): array
{
$dataClass = $this->getForm()->getConfig()->getDataClass();
$property = str_replace(['[', ']'], '', $propertyPath);
$propMetadata = $this->liveComponentMetadataFactory->createLivePropMetadata(
$dataClass,
$property,
new \ReflectionProperty($dataClass, $property),
new LiveProp()
);
$collectionClass = $propMetadata->collectionValueType()?->getClassName();
if ($collectionClass) {
return $this->deHydrateClass($collectionClass);
}
return [];
}
}
if this is the right solution, then i will make a PR to change the existing LiveCollectionTrait. With usage of the already existing deHydrateValue function in this package.
from ux.
Did it work "before" ? Do you know what changed ?
from ux.
Did it work "before" ? Do you know what changed ?
Until 2.7 its works, but after migrating to the latest version. I got now 2 actual issues.
- The ArrayCollection situation #1354 (comment)
- This AddCollectionItem problem with DTO. #1385 (comment)
This all is about this change;
"[BC BREAK]: LiveProp values are no longer automatically (de)hydrated through Symfony's serializer. Use LiveProp(useSerializerForHydration: true) to activate this. Also, a serializationContext option was added to LiveProp."
And useSerializerForHydratio:true don't fix every problem and result in other issues like circular references.
from ux.
Ok, can we see this via another angle ?
This all is about this change;
"[BC BREAK]: LiveProp values are no longer automatically (de)hydrated through Symfony's serializer. Use LiveProp(useSerializerForHydration: true) to activate this. Also, a serializationContext option was added to LiveProp."
So "before" 2.8, your values were dehydrated through the serializer and all worked as expected, and "after" 2.8 that did not work.
Can we see if there is something there to check / fix ?
from ux.
Related Issues (20)
- [TURBO] Impossible to redirect with anchor HOT 2
- [LiveComponent] Calling a property using a getter (i.e. computed) doesn't update component state (another component property)? HOT 5
- [TwigComponent] Profiler, memory consumption HOT 13
- The option "extra_options" does not exist. HOT 4
- [LiveComponent] How "defer" option work and the logic behind __invoke HOT 5
- [LiveComponent] getComponent is undefined if the controller loads very quickly as explained in the docs HOT 6
- [LiveComponent] `data-model` started throwing the "Invalid model name" error after upgrading to 2.15 HOT 1
- [LiveComponent] Can I rely on "busy" attribute to show a loading indicator outside a component?
- [Autocomplete] Doesn't load correctly on the first input HOT 1
- [LiveComponent] Updated values not submitted to the server properly HOT 7
- [Site] Add example for LazyImage with `srcset`
- [LiveComponent] Test helper doesn't work with localized components HOT 4
- [LiveComponent] Running `npm install --force` fails with "This is related to npm not being able to find a file." HOT 2
- Explicit re-render of a field when clicking a button HOT 3
- [TwigComponent] Examples in the CVA documentation do not work HOT 4
- [LiveComponent] Using ComponentWithFormTrait and switching forms doesn't keep form values. HOT 3
- Turbo restoration visit displays wrong AJAX HOT 2
- [LiveComponent] Forever Scroll HOT 11
- [TwigComponent] CVA - Allow passing array in classes HOT 3
- [Live] Use (de)hydration system for event payload HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ux.