smon

smon

Recommendations for custom live view upload validation

I am experimenting with live view uploads for the first time, and I was wondering what is the recommended way of doing customized upload validations.

My simple example: I have an interface to upload files to a certain directory, and I want to warn the users of duplicate file names in their upload before they hit the submit button. My consume_uploaded_entries/3 would catch these cases, but I am expecting larger files being uploaded, so some feedback earlier would be nice.

So I could evaluate my uploads assign against a list of existing files by…
… setting :valid to false in the appropriate Phoenix.LiveView.UploadEntry structs.
… generating an appropriate error list for errors in the Phoenix.LiveView.UploadConfig struct.

It feels a little bit like screwing around with Phoenix LiveView implementation internals. Is this the way to do it? Did I miss some kind of helper function?

Marked As Solved

smon

smon

Small update: In order to preserve the errors that got returned by Phoenix’ standard evaluation (size, type, upload count issues), I had to modify the function slightly.

defp mark_duplicate_names(%Phoenix.LiveView.UploadConfig{} = upload, existing_names) do
  processed_entries =
    upload.entries
    |> Enum.map(fn %Phoenix.LiveView.UploadEntry{} =
                      entry ->
      if entry.client_name in existing_names do
        Map.put(entry, :valid?, false)
      else
        entry
      end
    end)

  duplicate_name_errors =
    Enum.map(processed_entries, fn %Phoenix.LiveView.UploadEntry{} = entry ->
      previous_error? = Enum.any?(upload.errors, fn {ref, _msg} -> ref == entry.ref end)

      if entry.valid? or previous_error? do
        nil
      else
        {entry.ref, :name_duplicate}
      end
    end)
    |> Enum.reject(fn val -> is_nil(val) end)

  # Combine existing errors with additional ones evaluated here.
  errors = upload.errors ++ duplicate_name_errors

  upload
  |> Map.put(:entries, processed_entries)
  |> Map.put(:errors, errors)
end

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
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
RisingFromAshes
I've read in another post that it may be possible with a router helper - but I couldn't find an appropriate one, and tbh, I'm still just ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "eq...
New
vegabook
I'm brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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
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

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53578 245
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I fore...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement