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