[LiveComponent][Form] How to use constraints on the Entity's relationships? #2805
Replies: 2 comments
-
Ok looks like once again I missed something in the doc: https://symfony.com/bundles/ux-live-component/current/index.html#liveprop-for-entities-more-complex-data My entity is unpersisted at this point, I set the relation by hand in the controller: $payment = new Payment();
$payment->setLease($lease);
$payment->setPaymentMethod(PaymentMethod::ONLINE_PAYMENT);
$payment->setPaymentDate(CarbonImmutable::now());
$payment->setAmountPaid($lease->getRemainingAmountToPayForCurrentMonth());
$payment->setStatus(PaymentStatus::PAID); But since LiveComponent will never actually send the data of my payment as-is with the AJAX call, all the fields that are not in the form are lost, and even if they are not (adding an I have to go but I guess I have to keep the Another workaround could be to redo my controller logic in the LiveComponent: pass my Will report back, still open to suggestions in the meantime. Thanks. |
Beta Was this translation helpful? Give feedback.
-
The FormEvents route did not work, since even with the addition of the What I ended up doing is passing my Not sure if it's the best, but it seems to do the job. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there,
I'm transforming one of my forms to a LiveComponent but I am faced with an issue:
My form is handling a
Payment
entity, this entity has aLease
relationship:I want to make sure that when the user adds a new payment, they do not go over the rent for the month, so I have this constraint in my
amountPaid
field:This works as expected in my standalone regular form in my controller.
It, however, fails completely with LiveComponent:
From what I was able to trace, the issue is that when the form is updated on the frontend, my original
Payment
entity with all of its relationship is discarded and a partial of it is created instead.This is the first time
instantiateForm()
is called, with thePayment
i supplied from the template. No issues here.This is the second time
instantiateForm()
is called fromComponentWithFormTrait::submitForm()
(line 162), the lease is uninitalized. In fact, every field except the one I just modified (for "automatic validation") is unintialized.I believe this happens because when the data changes on the frontend, my Payment object in my form is changed to one that only has the data available in form hydrated in it, rather than being my original object updated with the form data. This is somewhat mentioned here but with no solution: https://symfony.com/bundles/ux-live-component/current/index.html#dynamically-updating-the-form-in-a-liveaction
I'm not sure what I can do here, except remove my constraint but I'd like to keep it. Is there a way for make LiveComponent retrieve my real payment and update it with
$this->formValues
when it re-renders?Like a normal controller with a form would do: get the payment from id in the URL (or something else), then hydrate it with the new values, then validate it.
I tried adding an hidden EntityType with my lease, to try and force LiveComponent to re-populate it, but that did not work because the payment is not fetched through Doctrine and the relationship cannot be lazy loaded.
I hope I make sense and would appreciate any help I can get on this.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions