How do we do error handling for the live_file_input direct to S3?

When we do a direct to S3 live_file_input with the changset validating that the file was uploaded

<.simple_form :let={f} for={@changeset} id="my-form" phx-target={@myself} phx-change="validate" phx-submit="save">
     <.input field={f[:doc_name]} type="text" />
    <.live_file_input upload={@uploads.mydoc} />

We have a changeset backing the above input fields in a schema. Now when the file is directly uploaded to S3, we must show an error message if the changeset fails the required validation for both fields. The text field works fine because the error message is handled automatically by liveview.

But how do we handle the live_file_input to ensure the file was actually uploaded? The changeset will not persist without the url being part of the changeset.

How do we provide interactive error message with file input just like the error message with the text input?

One option is the have the “required” attribute specified on the live_file_input tag which the browser then handle by itself natively and won’t let the form be submitted.

Here’s a helpful thread: Best way to handle LiveView upload "direct to S3" errors - #2 by mcrumm