Reset Files
Clears file inputs (<input type="file">) automatically after a successful submit re-renders the form.
Identifier: reset-files
Loaded by: auto-loaded after php artisan hotwire:install; publish only to customize with
php artisan hotwire:controllers reset-files.
Requirements
- No external dependencies.
- Turbo 8+ (
turbo:renderfor Drive/page-morph renders,turbo:frame-renderfor Turbo Frames).
How it works
- On
turbo:submit-end, records whether the submit of this element's form succeeded (HTTP 2xx/3xx). - On the following render (
turbo:renderor a relevantturbo:frame-render), if the submit succeeded and the re-rendered form has no field markedaria-invalid="true", the file input(s) are cleared. The controller may be mounted on the file<input>itself (how<hw:file>uses it), on the<form>, or on a wrapper.
The two-step success check matters: a 200 response that re-renders the form with validation errors reports
success on turbo:submit-end, so the aria-invalid guard is what actually distinguishes success from a failed
validation. <hw:file> renders aria-invalid="true" on invalid fields automatically; standalone usage must do
the same for the guard to work.
Frame events affect an instance only when their frame is the controller element, an ancestor that owns it, or a descendant it contains. Unrelated sibling frames do not consume an armed reset. A controller mounted on a page-level wrapper still observes descendant frame renders.
Usage
Add the controller to the form and mark it with data-reset-on-success="true":
<form
data-controller="reset-files"
data-reset-on-success="true"
action="/uploads"
method="post"
enctype="multipart/form-data"
>
@csrf
<input type="file" name="avatar" />
<input type="file" name="document" />
<button type="submit">Upload</button>
</form>
After the server responds successfully and Turbo applies the morph, file inputs are cleared automatically. This avoids the issue of file inputs retaining a reference to the previous file even after a DOM morph.
When to use
- Upload forms that remain on screen after submission (no redirect).
- Forms inside Turbo Frames that re-render after a successful submit.
- Any scenario where the form is re-rendered (Drive morph or frame render) and file inputs should be reset.