phoenix_live_view icon indicating copy to clipboard operation
phoenix_live_view copied to clipboard

How can I use `allow_upload` in nested form?

Open willyjie23 opened this issue 4 years ago • 1 comments

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

willyjie23 avatar Sep 06 '22 06:09 willyjie23

I need to use Enum.reduce assigned to the socket.

willyjie23 avatar Sep 06 '22 06:09 willyjie23