Skip to content

Commit 7cb9ced

Browse files
committed
Fixes #7823: Properly handle return_url when Save & Continue button is present
1 parent 97f0414 commit 7cb9ced

File tree

6 files changed

+50
-11
lines changed

6 files changed

+50
-11
lines changed

docs/release-notes/version-3.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Enhancements
66

77
* [#7751](https://github.com/netbox-community/netbox/issues/7751) - Get API user from LDAP only when `FIND_GROUP_PERMS` is enabled
8+
* [#7823](https://github.com/netbox-community/netbox/issues/7823) - Fix issue where `return_url` is not honored when 'Save & Continue' button is present
89
* [#7885](https://github.com/netbox-community/netbox/issues/7885) - Linkify VLAN name in VLANs table
910
* [#7892](https://github.com/netbox-community/netbox/issues/7892) - Add L22-30 power port & outlet types
1011
* [#7932](https://github.com/netbox-community/netbox/issues/7932) - Improve performance of the "quick find" function

netbox/project-static/dist/netbox.js

Lines changed: 7 additions & 7 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: 1 addition & 1 deletion
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: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,32 @@
1-
import { getElements, scrollTo } from '../util';
1+
import { getElements, scrollTo, isTruthy } from '../util';
2+
3+
/**
4+
* When editing an object, it is sometimes desirable to customize the form action *without*
5+
* overriding the form's `submit` event. For example, the 'Save & Continue' button. We don't want
6+
* to use the `formaction` attribute on that element because it will be included on the form even
7+
* if the button isn't clicked.
8+
*
9+
* @example
10+
* ```html
11+
* <button type="button" return-url="/special-url/">
12+
* Save & Continue
13+
* </button>
14+
* ```
15+
*
16+
* @param event Click event.
17+
*/
18+
function handleSubmitWithReturnUrl(event: MouseEvent): void {
19+
const element = event.target as HTMLElement;
20+
if (element.tagName === 'BUTTON') {
21+
const button = element as HTMLButtonElement;
22+
const action = button.getAttribute('return-url');
23+
const form = button.form;
24+
if (form !== null && isTruthy(action)) {
25+
form.action = action;
26+
form.submit();
27+
}
28+
}
29+
}
230

331
function handleFormSubmit(event: Event, form: HTMLFormElement): void {
432
// Track the names of each invalid field.
@@ -38,6 +66,15 @@ function handleFormSubmit(event: Event, form: HTMLFormElement): void {
3866
}
3967
}
4068

69+
/**
70+
* Attach event listeners to form buttons with the `return-url` attribute present.
71+
*/
72+
function initReturnUrlSubmitButtons(): void {
73+
for (const button of getElements<HTMLButtonElement>('button[return-url]')) {
74+
button.addEventListener('click', handleSubmitWithReturnUrl);
75+
}
76+
}
77+
4178
/**
4279
* Attach an event listener to each form's submitter (button[type=submit]). When called, the
4380
* callback checks the validity of each form field and adds the appropriate Bootstrap CSS class
@@ -54,4 +91,5 @@ export function initFormElements(): void {
5491
submitter.addEventListener('click', (event: Event) => handleFormSubmit(event, form));
5592
}
5693
}
94+
initReturnUrlSubmitButtons();
5795
}

netbox/templates/dcim/interface_edit.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ <h5 class="offset-sm-3">Custom Fields</h5>
5151
{% block buttons %}
5252
<a href="{{ return_url }}" class="btn btn-outline-danger">Cancel</a>
5353
{% if obj.pk %}
54-
<button type="submit" formaction="?return_url={% url 'dcim:interface_edit' pk=obj.pk %}" class="btn btn-outline-primary">Save & Continue Editing</button>
54+
<button type="button" return-url="?return_url={% url 'dcim:interface_edit' pk=obj.pk %}" class="btn btn-outline-primary">Save & Continue Editing</button>
5555
<button type="submit" name="_update" class="btn btn-primary">Save</button>
5656
{% else %}
5757
<button type="submit" name="_addanother" class="btn btn-outline-primary">Create & Add Another</button>

netbox/templates/virtualization/vminterface_edit.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ <h5 class="offset-sm-3">Custom Fields</h5>
4646
{% block buttons %}
4747
<a href="{{ return_url }}" class="btn btn-outline-danger">Cancel</a>
4848
{% if obj.pk %}
49-
<button type="submit" formaction="?return_url={% url 'virtualization:vminterface_edit' pk=obj.pk %}" class="btn btn-outline-primary">Save & Continue Editing</button>
49+
<button type="button" return-url="?return_url={% url 'virtualization:vminterface_edit' pk=obj.pk %}" class="btn btn-outline-primary">Save & Continue Editing</button>
5050
<button type="submit" name="_update" class="btn btn-primary">Save</button>
5151
{% else %}
5252
<button type="submit" name="_addanother" class="btn btn-outline-primary">Create & Add Another</button>

0 commit comments

Comments
 (0)