# `Waffle.LiveView`
[🔗](https://github.com/elixir-waffle/waffle/blob/v2.1.0/lib/waffle/live_view.ex#L1)

Provides helpers for Phoenix LiveView file upload handling.

# `consume_entry`

```elixir
@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)
    )

# `validate`

```elixir
@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`:

```heex
<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.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
