mendrik

mendrik

Calling assign on validation errors in file upload

Hello,

I’m a bit at a loss with file uploads in Phoenix liveview. Everything works great, except that when I get validation errors I am unable to assign a value inside the socket. I have a custom progress handler (which also works just fine), but when user tries to upload too many files at once, I would like to cancel the uploads and set :upload_progress back to 0. Not only is there some initial progress, but I’m also unable to reset it back to 0. I see the flash error but assign() seems to do nothing here.

  @impl true
  def handle_event("validate", _params, %{assigns: assigns} = socket) do
    photoset = assigns.uploads.photoset
    {:noreply, socket |> update_socket_based_on_errors(photoset)}
  end

  defp update_socket_based_on_errors(socket, photoset) do
    if !Enum.empty?(photoset.errors) do
      Enum.reduce(photoset.errors, socket, fn error, acc_socket ->
        handle_error(error, acc_socket)
      end)

      Enum.each(photoset.entries, &cancel_upload(socket, :photoset, &1.ref))
      socket |> assign(:upload_progress, 0) # this doesn't update the HTML correctly, no error is thrown.
    else
      socket
    end
  end

  defp handle_error({picture, :too_large}, socket) do
    socket
    |> put_flash!(:error, "File size of photo #{picture} is too large")
  end

Marked As Solved

mendrik

mendrik

Actually nevermind, cancelling it the right way worked:

  socket =
        Enum.reduce(photoset.entries, socket, fn entry, acc_socket ->
          cancel_upload(acc_socket, :photoset, entry.ref)
        end)

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement