Skip to content

[LiveComponent] Fix expanded choices and checkboxes in forms #221

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

Merged
merged 44 commits into from
Feb 7, 2022

Conversation

Lustmored
Copy link
Contributor

Q A
Bug fix? yes
New feature? no
Tickets
License MIT

When using live component with forms that contains expanded choices list (rendered as a list of radio buttons) extracting form values returns array of nulls (one per choice), while frontend expects single value.

Therefore after calling first update and then trying to call update on expanded field controller throws (The property used in data-model="${propertyPath}" was never initialized. Did you forget to add exposed={"${lastPart}"} to its LiveProp?).

This simple change fixes the issue. I have also added simple test case for a few basic form types.

@Lustmored Lustmored force-pushed the fix-expanded-choices branch from 2e7cc8c to 653b1a5 Compare January 14, 2022 11:30
Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

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

A few comments - but this is great! Especially the test - that fixes a bit missing hole.

'bar' => 2,
],
'expanded' => true,
])
Copy link
Member

Choose a reason for hiding this comment

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

What about a ChoiceType with expanded true AND multiple true (i.e. checkboxes)? Is that correctly handled? Or does the check for $child->vars['expanded'] mess up that case? I'm genuinely just not sure - I'd love it if you could verify by testing that in a real project.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for quick review! I've followed your suggestions in other places and allowed myself to resolve them.

You are probably right about multiple option - I have missed that. But when experimenting with component with form I've found that CheckboxType was also misbehaving (haven't got enough time yet to investigate it or write a test case for bug report), therefore my intuition tells me that expanded and multiple (that expands into multiple checkboxes) isn't working right now anyways.

My intent is to spend my OSS Friday next week to investigate problems with checkboxes and either fix them or provide meaningful bug report. I can do that as part of this PR or the next one if you prefer to have at least fix for expanded merged earlier :)

Copy link
Member

Choose a reason for hiding this comment

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

Thank you!

My intent is to spend my OSS Friday next week to investigate problems with checkboxes and either fix them or provide meaningful bug report.

This sounds wonderful. Tbh, I believe I wrote this original code that has the bug(s) in it. I knew I needed to handle the checkbox case, but I'm not certain that I dove deeply (or thought deeply enough) about all of these expanded and multiple situations. Someone needs to dive deep and look at the whole problem. If you can do that, it would be wonderful.

I can do that as part of this PR or the next one if you prefer to have at least fix for expanded merged earlier :)

Let's get everything fixed up on this PR :)

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated PR with fixes for single checkboxes (their null value was getting lost after any update) and also expanded and multiple choice fields.

As it is right now all basic data types (except FileType, which is another story) that I've tested are working as intended.

To check out how checkbox inputs are working I used https://github.com/weaverryan/live-demo - as it is right now writing anything in registration form in e-mail first and then clicking checkbox throws in JS. With this PR it synchronizes data between frontend and backend.

To see expanded+multiple in action you can use the same registration form from my fork: https://github.com/Lustmored/live-demo

@Lustmored Lustmored changed the title [LiveComponent] Fix expanded choices form values [LiveComponent] Fix expanded choices and checkboxes in forms Jan 21, 2022
Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

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

This is very nice work on a super complex problem which, thanks to this PR, I can finally wrap my head around :). A few explanations, tests and refactorings are needed - but this is well on its way.

Thanks!

@Lustmored
Copy link
Contributor Author

I think I have finished all refactoring and have added enough tests to cover all cases. The <select multiple> became trivial once checkbox logic was in place and has a few tests of it's own right now.

I have tested this on locally modified live twig demo (it is a bit behind with changes here btw.) - my real world project usage is right now a bit too hacky (to make it work without this PR).

I think I'm ready for the next wave of reviews 😀

@Lustmored Lustmored requested a review from weaverryan February 4, 2022 13:11
Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

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

This is looking GREAT. I added quite a few comments because it's a complex feature and I want to get it perfect. However, I am quite confident now that you've got this fully-working - the JS test suite is particularly awesome. Thank you!

'data' => true,
])
->add('file', FileType::class)
->add('hidden', HiddenType::class)
Copy link
Member

Choose a reason for hiding this comment

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

A lot of good field types included here 👍

[
'checkbox' => null,
]
);
Copy link
Member

Choose a reason for hiding this comment

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

This extra variable is confusing. I believe it is because we want to ASSERT that the checkbox field equals null. But when we SEND the data to the server, we want this field to be omitted? Is that right?

If so, are you sure that's correct? With the new way of sending the data as JSON, if I have a form with an unchecked checkbox, the initial data sent to the frontend will store this value as null (and also, if I check and uncheck the box, the model will be set to null). And we DO now send null values to the Ajax calls.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fun fact is - in pure HTML world unchecked checkboxes would not be sent to the server at all and before parsing data as JSON live component was following those trails and it looked like perfect solution.

BUT since data is now sent as JSON and as Symfony Form component is handling nulls as unchecked state (as my updated test shows) I believe it will be simpler to just send all nulls.

This also simplifies this test case a lot by removing variable that probably only I knew why existed :)

@Lustmored
Copy link
Contributor Author

Thanks @weaverryan for the review - I will try to work on it during weekend, as it seems like mostly tidying up, so shouldn't be difficult 👍

@Lustmored Lustmored force-pushed the fix-expanded-choices branch from 6689e66 to 7605821 Compare February 5, 2022 09:54
@Lustmored
Copy link
Contributor Author

@weaverryan I think I have done most of your change request. I have resolved some of them to ease tracking what was done and what's left. In a few I left comments where explanation was needed or I had doubts about change suggestions 👍

@Lustmored Lustmored requested a review from weaverryan February 5, 2022 09:58
Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

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

WONDERFUL work. You rock @Lustmored - this was a huge, complex problem and you were a joy to work with. Let's get this merged, and if we missed any edge cases, we'll find out :).

@weaverryan weaverryan merged commit b355eb9 into symfony:2.x Feb 7, 2022
@Lustmored
Copy link
Contributor Author

Thank you @weaverryan 👍 working with you and on this project is also a pleasure, so I surely will be back - I love how enabling components+live-components are architecturally in my projects already. See you around soon 😄

@Lustmored Lustmored deleted the fix-expanded-choices branch March 11, 2022 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants