bigskysoftware / bigskysoftware/htmx

form submission without a file selected

Open
#2,136 1 comment 3 reactions 0 assignees View on GitHub
htmx 4
Dominant language
JavaScript
Stars
49.4k
Forks
1.7k
Avg merge
3d 19h
Merged PRs (30d)
31

Description

I've noticed a discrepancy in how forms behave when using HTMX compared to standard HTML forms. Specifically, this difference arises with file input elements. In a standard HTML form, if a file input element is left empty and the form is submitted, the input's name attribute still appears in the HTTP request's body. However, with HTMX, the name attribute doesn't show up in the body of the HTTP request if no file is selected.

Here's an example to illustrate:

Standard HTML Form Example:

```


```

In this case, submitting the form without selecting a file results in the HTTP body including the 'file' attribute name but no binary data:

```
-----------------------------10335236603037115181545093065
Content-Disposition: form-data; name="file"; filename=""
Content-Type: application/octet-stream

-----------------------------10335236603037115181545093065--
```

HTMX Form Example:

```




```

Here, submitting the form without a file selection results in an empty HTTP body.

The issue seems to be resolved by checking for an empty array `if (Array.isArray(value) && value.length)` in the `makeFormData` function within htmx.js.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.