-
-
Notifications
You must be signed in to change notification settings - Fork 312
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
[LiveComponent] Custom form errors not displayed when invoking live action #1123
Comments
Hey Ivo! Real quick, does it help if you submit the form first in your LiveAction? use ComponentWithFormTrait;
#[LiveAction]
public function save()
{
$this->submitForm(0;
// Attempting to add a global form error
$this->getForm()->addError(new \Symfony\Component\Form\FormError('General error'));
// Attempting to add a specific field error
$this->getForm()->get('description')->addError(new \Symfony\Component\Form\FormError('Form field error'));
} From the code I see above, you were not calling this. When you don't submit a form in your LiveAction, it is submitted for your AFTER the LiveAction. So it's possible that you're adding the error... then it's immediately cleared out when the form submits a moment later. Cheers! |
Hi Ryan! It doesn't, unfortunately. I also tried that before, now I had totally forgotten to include it in the reproducible example. Let me know if there is anything else I can test. Cheers |
The problem might be that try {
$this->submitForm();
} (catch UnprocessableEntityHttpException $e) {
// add your custom error here
throw $e;
} |
Thanks for the quick reply. Let me provide a bit more context (I didn't provide it initially, as I believed the issue was not related). In my forms, after Let's say I am building a signup form. I have in my service a validation that checks for instance whether the given email is unique. If it isn't, it throws an Tldr; while the majority of invalid fields can be caught using the form validations, I need to catch service Simplified example: use ComponentWithFormTrait;
#[LiveAction]
public function save()
{
$this->submitForm();
// form should be valid at this stage
try {
$this->userService->create($this->dto); // let's say we initially created the form with a dto
} catch (EmailAlreadyInUseException) {
// Attempting to add a global form error
$this->getForm()->addError(new \Symfony\Component\Form\FormError('General error: Email already in use'));
// Attempting to add a specific field error
$this->getForm()->get('email')->addError(new \Symfony\Component\Form\FormError('Field error: Email already in use'));
}
} |
Thank you for this issue. |
Friendly reminder that this issue exists. If I don't hear anything I'll close this. |
Hey, I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen! |
This issue is still relevant, I'm facing the same problem |
Unfortunately I am still facing this as well. Did you had a chance to look further into the context I have added last time @weaverryan? |
Did you guys tried to throw a UnprocessableEntityExcetion there ? |
Yes, I'm throwing an When calling some live action method, the first call is to A workaround that worked for me was to reset the Taking the example from @IvoPereira
|
Hi there.
I believe I encountered an issue with the Symfony UX's
ComponentWithFormTrait
where form errors are not displayed when a live action is invoked from a component template.To give a brief context: I have some business logic that throws some very specific Domain Errors that I would need to map and display to the user.
I'm using the
ComponentWithFormTrait
to handle forms in my live component.When invoking a live action, I'm trying to add both global form errors and specific field errors. However, despite adding these errors, they are not displayed in the form.
Reproduction Steps:
ComponentWithFormTrait
.Minimal Reproducible Code:
Component:
Template:
If it makes any difference, my form is using a data-transfer object for the form data. I have noticed that with a regular Symfony action+Twig template this does work.
I suspect that somehow the form instance might not be the same getting rendered?
Is this a known issue, or is there a recommended way to force the form errors to display in this scenario?
The text was updated successfully, but these errors were encountered: