A LiveView page's flow, data, and changeset/assigns vs hidden fields


I’d like confirmation or correction regarding my high-level approach to form data in a LiveView page.

There are often various fields in the data that we don’t show on the form, but need to be saved back to the database. In the case of adding a new record, I’ve been merging those fields onto an empty schema struct and then calling the context’s “changes” function to get a changeset. I do it this way so that “add” is consistent with “edit” when the page loads (except that in the case of edit the data comes from the database and I just pass it right to the changes function).

I then use a hidden field to put those on the page so that they come back in the form data that I get when the user hits the save button.

  • Is this approach correct?
  • Should I be sticking these non-visible fields in the socket.assigns instead of in hidden fields and then somehow merge them back into the data submitted from the form? If so, what’s the right way to merge them in on the submit event? Modifying the raw form data looks like a bad idea to me.
1 Like