Skip to content

Commit 2830939

Browse files
committed
Prevent form validation and give a helpful error message when an user tries to submit a form with a file upload field that is above the maximum file size.
1 parent 721ced6 commit 2830939

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
- Fix a bug that occured when no `database_url` was provided in the configuration file. SQLPage would generate an incorrect default SQLite database URL.
1010
- Add a new `background_color` attribute to the [card](https://sql.ophir.dev/documentation.sql?component=card#component) component to set the background color of the card.
1111
- new handlebars helper for [custom components](https://sql.ophir.dev/custom_components.sql): `{{app_config 'property'}}` to access the configuration object from the handlebars template.
12+
- Prevent form validation and give a helpful error message when an user tries to submit a form with a file upload field that is above the maximum file size.
13+
- ![file upload too large](https://github.com/lovasoa/SQLpage/assets/552629/1c684d33-49bd-4e49-9ee0-ed3f0d454ced)
1214

1315
## 0.23.0 (2024-06-09)
1416

sqlpage/sqlpage.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,23 @@ function sqlpage_map() {
152152
}
153153
}
154154

155+
function sqlpage_form() {
156+
const file_inputs = document.querySelectorAll("input[type=file][data-max-size]");
157+
for (const input of file_inputs) {
158+
const max_size = +input.dataset.maxSize;
159+
input.addEventListener("change", function() {
160+
input.classList.remove("is-invalid");
161+
input.setCustomValidity("");
162+
for (const {size} of this.files) {
163+
if (size > max_size){
164+
input.classList.add("is-invalid");
165+
return input.setCustomValidity(`File size must be less than ${max_size/1000} kB.`);
166+
}
167+
}
168+
});
169+
}
170+
}
171+
155172
function get_tabler_color(name) {
156173
return getComputedStyle(document.documentElement).getPropertyValue('--tblr-' + name);
157174
}
@@ -175,5 +192,6 @@ add_init_function(function init_components() {
175192
sqlpage_table();
176193
sqlpage_map();
177194
sqlpage_card();
195+
sqlpage_form();
178196
load_scripts();
179197
});

sqlpage/templates/form.handlebars

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@
104104
{{~#if autofocus}}autofocus {{/if~}}
105105
{{~#if disabled}}disabled {{/if~}}
106106
{{~#if readonly}}readonly {{/if~}}
107+
{{~#if (eq type "file")}}
108+
data-max-size="{{app_config "max_uploaded_file_size"}}"
109+
{{/if~}}
107110
/>
108111
{{#if suffix}}<span class="input-group-text">{{suffix}}</span>{{/if}}
109112
</div>

0 commit comments

Comments
 (0)