symfony / ux

Symfony UX initiative: a JavaScript ecosystem for Symfony
https://ux.symfony.com/
MIT License
834 stars 302 forks source link

[LiveComponent] Bug with ChoiceType in Form #759

Open nklth opened 1 year ago

nklth commented 1 year ago

Hi,

I am currently trying out Symfony Forms and Live components to reduce the need for Javascript. The first try outs went quite well and I would love to use this in the future!

But I have found a bug which seemed random at first (my form is a little bit more complicated using the choices type to add additional fields to the form), but I can now reproduce it in a simple form:

Requirements:

I have a form with a choice type and 3 choices, and an additional text field with a NotBlank validation.

Steps to reproduce:

Everything works fine, before the submit is done.

What I have found in the Chrome inspector is: After the first step the data-live-data-value attribute looks like this: {"test":{"choices":["zero","one"],"name":""},"isValidated":false,"validatedFields":["test.choices"]} ==> The choices field is an array, as expected

After step 3 it looks like this: "{"test":{"choices":{"1":"one"},"name":""},"isValidated":true,"validatedFields":[]} ==> The choices field now is a json value

And finally after step 4 it looks like this: {"test":{"choices":"zero","name":""},"isValidated":true,"validatedFields":[]} ==> The choices field now has a string value

And this string value seems to be submitted to the form which triggers the invalid value error.

I have created a reproducer with this simple form here if needed: https://github.com/nklth/live-component-form-choices

cgo123 commented 11 months ago

Bump

cgo123 commented 11 months ago

Hi i found a workaround: use the PostMount and unset the initialFormData: The prefilled form works fine:

`

[PostMount]

public function postMount(): void
{
    $this->initialFormData = [];
}

protected function instantiateForm(): FormInterface
{
    return $this->createForm(YourFormType::class, $this->initialFormData);
}

`

carsonbot commented 5 months ago

Hey, thanks for your report! There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

carsonbot commented 5 months ago

Friendly ping? Should this still be open? I will close if I don't hear anything.

carsonbot commented 4 months ago

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!