Provides helpers for Phoenix LiveView file upload handling.
Summary
Functions
Converts LiveView Upload into a %Waffle.File{}.
Validates a LiveView upload entry's filename before consumption.
Functions
@spec consume_entry(live_view_file(), live_view_upload_entry(), link_mode()) ::
{:ok, Waffle.File.t()}
Converts LiveView Upload into a %Waffle.File{}.
Options
:hard_link(default) - creates a hard link under a new tmp path to skip on unnecessary filesystem IO:copy- copies file to a new location instead of creating a hard link
Examples
user_params =
socket
|> consume_uploaded_entries(:avatar, &Waffle.LiveView.consume_entry/2)
|> then(fn
[] -> user_params
[file | _] -> Map.put(user_params, "avatar", file)
end)
updated_user =
socket.assigns.user
|> Accounts.User.changeset(user_params)
|> MyApp.Repo.update()to use :copy mode
consume_uploaded_entries(
socket,
:avatar,
&Waffle.LiveView.consume_entry(&1, &2, :copy)
)
@spec validate(live_view_upload_entry()) :: :ok | {:error, Waffle.LiveView.Error.t()}
Validates a LiveView upload entry's filename before consumption.
Configure it as the :validator callback in allow_upload/3:
allow_upload(socket, :avatar,
accept: ~w(.jpg .jpeg .png),
validator: &Waffle.LiveView.validate/1
)Display Waffle validation errors using Phoenix.Component.upload_errors/2:
<div :for={entry <- @uploads.avatar.entries}>
<p :for={%Waffle.LiveView.Error{} = error <- upload_errors(@uploads.avatar, entry)}>
{error.message}
</p>
</div>Built-in LiveView upload errors need separate handling.