PrincetonPoh

PrincetonPoh

Liveview presign_upload() for external uploads triggering even when changeset is NOT valid

I’m trying to upload a music file. I’ve created a music struct. It takes a title (string) and file_path (string)

I create a form which requires a title and a file upload.

What I expect is for the form to throw errors when 1) the upload is empty or 2) the title is empty.

The problem:
Case 1: title and file are empty. Only the error “empty title” is shown. NO ERROR for missing upload is shown
Case 2: there’s file input but no title. When I press the upload button, presign_upload() is called and the file is sent to the cloud even though my title is empty.

Both cases are not idea. This seems like a pretty basic use case. Am I missing something?

It appears that this guy was trying to implement it too but failed in the end.

Marked As Solved

LostKobrakai

LostKobrakai

I’d strongly suggest you don’t depend on a file only being uploaded if the form is valid. There’s simply no solution to atomically persist data in the db and have an upload persisted in a different system, so pretending it would work like that is not going to be great.

The workflow I generally suggest is uploading to a temp. directory, which is regularly cleaned up (e.g. S3 can do so with livecycle configuration). Then persist the data in the db pointing to the temporary file and within the same transaction queue work (e.g. using oban) to move the file to a different path, where it won’t be deleted. That way you don’t need to care if the upload happened a few seconds or minutes before the form was submitted.

However the issue around changeset errors being a completely separate system to the errors shown for LV uploads is indeed problematic. I’ve been running into that myself as well.

Also Liked

PrincetonPoh

PrincetonPoh

Thank you for this. It’s helpful for me to conceptualise this at a more fundamental level.

Oh wow, is this the industry standard to basically set up a “routine clean up” system?

How have you dealt with it? I think solving this issue will at least reduce the chances of redundant uploads to S3.

PrincetonPoh

PrincetonPoh

Ok, thank you for your help!

For anyone else reading, I tried to solve the Case 2 problem above with a simple case do in my presign_upload(). It was a failure as i need to surgically manipulate the socket to edit the errors so that the usual handle events can validate my forms. I shall await a more skilful individual to navigate this :')

krishandley

krishandley

My solution to work around this is having two forms.

A normal form with a quick hack to trigger the live_file_input

And a second form with the uploads in to submit manually after the normal form has been saved

<form phx-submit="SUBMIT" ...>
  ...
  <!-- <div>
    // Some kind of hook or custom web component
    const other = document.querySelector('form[phx-submit="SUBMIT_UPLOADS"] input[name="foo"]');
    your_button.addEventListener("click", () => other.click());
  </div> -->
</form>

<form phx-submit="SUBMIT_UPLOADS" ...>
  <.live_file_input upload={@uploads.foo} />
  <button :if={@foo_done} type="submit" phx-mounted={JS.dispatch("click")}>Submit</button>
</form>

Where Next?

Popular in Questions Top

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
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
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement