martinjanda

martinjanda

How add error to @uploads for live_file_input?

I have a form that uses liveview file upload. I would like to check that the user has selected the file and if not, display an error.

In the heex file I use .live_file_input and then display the error in the style:

<%= for err <- upload_errors(@uploads.input_file) do %>
   <p class="alert alert-danger"><%= error_to_string(err) %></p>
<% end %>

Is there any way to add a custom error to @uploads? Alternatively, how to show the error if the user has not selected the file?

Thanks

Most Liked

martinjanda

martinjanda

I solved it by adding a custom variable @custom_error and checking if a file is selected during the save event.

In ex:

if Enum.empty?(uploaded_files) do
      socket = assign(socket, :custom_errors, ["File must be selected!"])
      {:noreply, socket}
    else
      ...

In Heex:

<%= if @custom_errors != [] do %>
  <.error :for={msg <- @custom_errors}><%= msg %></.error>
<% end %>

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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 44265 214
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

We're in Beta

About us Mission Statement