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

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

We're in Beta

About us Mission Statement