In my socket assigns I have a list of evenings (structs).
For every evening in the list, I render a live component that contains a form relative to that evening.
There is also a button that adds an evening to the database and assigns the updated list of evenings to the socket.
When the page is first rendered, I can see every form without problem.
When I click the button to add an evening, the last form in the list “disappears”: all the input tags are removed from the html and the labels are emptied.
If I refresh the page, I can see everything.
I wrote a test to resolve this, but it always passes, and if I inspect the page with open_browser everything is ok.
I cannot understand what’s happening here, any help is appreciated.
I am using Phoenix 1.7.11 and LiveView 0.20.2
Try checking the id of each rendered forms is unique. It sounds like the handle_event when an action is performed on one of the forms clears out the others.
If that doesn’t help, show your form rendering code and you might be able to get better help.
I managed to create the simplest example I could to show this behavior. I published this example at zagoli/FormDisappearing: Why.
It is a simple LiveView mostly generated with Phoenix generators.
Visiting / we can see a quite simple LiveView: there is one form for every student in the database where we can edit the student’s name.
Each form is inside a live component with a unique id.
It’s essentially because the input’s share the same id, across the forms.
The TLDR is that this is a HTML naughty and LV doesn’t know which input has been updated so it wipes the others.
To ‘resolve’ your problem, ensure your input in core_components.ex has a unique id (it uses the name to work out the field on the form, so you don’t want to fiddle with that)
There is probably a better way of generating a unique ID, but below I have attached an example.
The better way to handle this would be to use to_form(data, id: unique_prefix), which would make the id generation be unique without messing with components.