phoenix_live_view
phoenix_live_view copied to clipboard
How can I use `allow_upload` in nested form?
How can I assign allow_upload in nested form?
I have tried many ways but just get errors.
Have any suggestions?
This code is from the official documentation
@impl true
def update(assigns, socket) do
{:ok,
socket
|> assign(assigns)
|> allow_upload/3}
end
This code is what I want to do
@impl true
def update(%{changeset: %{changes: changes}} = assigns, socket) do
uploads_list =
for m <- changes.meter_readings do
name = :attachment
ref = "phx-#{m.changes.meter_id}"
uploads = %{}
opts = [
accept: ~w(.jpg .jpeg .png),
max_entries: 1
]
upload_config = UploadConfig.build(name, ref, opts)
new_uploads =
uploads
|> Map.put(name, upload_config)
|> Map.update(@refs_to_names, %{ref => name}, fn refs -> Map.put(refs, ref, name) end)
%{uploads: new_uploads}
end
{:ok,
socket
|> assign(assigns)
|> assign(uploads_list: uploads_list)} // loop assign to the nested form
end
Please tell me what I'm doing wrong, thanks
I need to use Enum.reduce assigned to the socket.