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

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, "equa...
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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 55125 245
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New

We're in Beta

About us Mission Statement