Skip to content

Commit 6547a16

Browse files
Fixes #20398: Rely on browser-native form field validation (#20401)
1 parent 07a53c8 commit 6547a16

File tree

5 files changed

+12
-47
lines changed

5 files changed

+12
-47
lines changed

netbox/project-static/dist/netbox.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

netbox/project-static/dist/netbox.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

netbox/project-static/src/forms/elements.ts

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,23 @@
1-
import { getElements, scrollTo } from '../util';
1+
import { getElements } from '../util';
22

3-
function handleFormSubmit(event: Event, form: HTMLFormElement): void {
3+
function handleFormSubmit(): void {
44
// Automatically select all options in any <select> with the "select-all" class. This is useful for
55
// multi-select fields that are used to add/remove choices.
66
for (const element of getElements<HTMLOptionElement>('select.select-all option')) {
77
element.selected = true;
88
}
9-
10-
// Track the names of each invalid field.
11-
const invalids = new Set<string>();
12-
13-
for (const element of form.querySelectorAll<FormControls>('*[name]')) {
14-
if (!element.validity.valid) {
15-
invalids.add(element.name);
16-
// If the field is invalid, but doesn't contain the .is-invalid class, add it.
17-
if (!element.classList.contains('is-invalid')) {
18-
element.classList.add('is-invalid');
19-
}
20-
} else {
21-
// If the field is valid, but contains the .is-invalid class, remove it.
22-
if (element.classList.contains('is-invalid')) {
23-
element.classList.remove('is-invalid');
24-
}
25-
}
26-
}
27-
28-
if (invalids.size !== 0) {
29-
// If there are invalid fields, pick the first field and scroll to it.
30-
const firstInvalid = form.elements.namedItem(Array.from(invalids)[0]) as Element;
31-
scrollTo(firstInvalid);
32-
33-
// If the form has invalid fields, don't submit it.
34-
event.preventDefault();
35-
}
369
}
3710

3811
/**
39-
* Attach an event listener to each form's submitter (button[type=submit]). When called, the
40-
* callback checks the validity of each form field and adds the appropriate Bootstrap CSS class
41-
* based on the field's validity.
12+
* Attach event listeners to each form's submit/reset buttons.
4213
*/
4314
export function initFormElements(): void {
4415
for (const form of getElements('form')) {
45-
// Find each of the form's submitters. Most object edit forms have a "Create" and
46-
// a "Create & Add", so we need to add a listener to both.
16+
// Find each of the form's submit buttons.
4717
const submitters = form.querySelectorAll<HTMLButtonElement>('button[type=submit]');
4818
for (const submitter of submitters) {
4919
// Add the event listener to each submitter.
50-
submitter.addEventListener('click', (event: Event) => handleFormSubmit(event, form));
20+
submitter.addEventListener('click', () => handleFormSubmit());
5121
}
5222

5323
// Initialize any reset buttons so that when clicked, the page is reloaded without query parameters.

netbox/templates/dcim/htmx/cable_edit.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ <h2 class="col-9 offset-3">{% trans "Cable" %}</h2>
6565
<div class="col-md-4">
6666
{{ form.length_unit }}
6767
</div>
68-
<div class="invalid-feedback"></div>
6968
</div>
7069
{% render_field form.tags %}
7170
</div>

netbox/utilities/templates/form_helpers/render_field.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@
5252
<div class="form-text text-danger">
5353
{% for error in field.errors %}{{ error }}{% if not forloop.last %}<br />{% endif %}{% endfor %}
5454
</div>
55-
{% elif field.field.required %}
56-
<div class="invalid-feedback">
57-
{% trans "This field is required" %}.
58-
</div>
5955
{% endif %}
6056

6157
{# Help text #}

0 commit comments

Comments
 (0)