mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-11 12:46:49 +00:00
fix(instances): a refused VM create form comes back with what was entered
When the server refused a VM create submission (a field error, a busy resource, a reused submission key, an existing VM name, a missing MAC for a new disk), the page came back on its first tab with the flavor dialog closed and every entered value gone: the hand-written fields showed fixed defaults, and the errors sat in a block at the top of the page, outside #messages and tied to no field. The view builds one NewVMForm per create form on the page (custom, template, one per flavor; create_forms), each with its own defaults; a hidden form_id, which is also the prefix of the form's field ids, names the submitted one, and it comes back bound. Every field renders from its own form and is marked by two small template tags (form_fields: error_attrs, field_error) when refused; a summary at the top of the form lists all of its errors, also those of fields it keeps hidden. The submitted tab or flavor dialog, marked data-form-id, reopens with any "Advanced" section that holds an error; a reopened dialog shows the messages too; the template form gets its pool, template and storage back. base.html's error focus is a function that walks the places an error can be, and a page can call it once it is set up, as the create page does. form_id changes nothing in how a POST is processed; one of an unknown form (a flavor deleted since) has its errors in the messages. The default video now applies to every create form. The custom form's disks and networks are not restored yet.
This commit is contained in:
parent
35468bc0fd
commit
dba41f97f3
9 changed files with 640 additions and 164 deletions
|
|
@ -34,7 +34,7 @@
|
|||
<link rel="shortcut icon" href="{% static 'favicon.ico' %}">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<body{% block body_attrs %}{% endblock %}>
|
||||
|
||||
{% if request.user.is_authenticated %}
|
||||
{% include 'navbar.html' %}
|
||||
|
|
@ -67,16 +67,26 @@
|
|||
<!-- JavaScript Cookie -->
|
||||
<script src="{% static 'js/js.cookie.js' %}"></script>
|
||||
<script>
|
||||
// after a refused submission, start at the error: the first visible
|
||||
// invalid field, else the error message
|
||||
(function () {
|
||||
const field = [...document.querySelectorAll(".is-invalid")].find(e => e.offsetParent !== null);
|
||||
const target = field || document.querySelector("#messages .alert-danger");
|
||||
if (target) {
|
||||
if (!field) target.tabIndex = -1;
|
||||
target.focus();
|
||||
// After a refused submission, start at the error. The places an error
|
||||
// can be, most specific first: a refused field, its form's summary, an
|
||||
// open dialog's copy of the messages (the page's are behind it), the
|
||||
// page's messages. A page that sets itself up first has
|
||||
// data-error-focus="manual" and calls this when it is ready.
|
||||
const ERROR_PLACES = [".is-invalid", ".form-errors", ".modal.show .modal-messages .alert-danger",
|
||||
"#messages .alert-danger"];
|
||||
|
||||
function focusFirstError() {
|
||||
for (const selector of ERROR_PLACES) {
|
||||
const target = [...document.querySelectorAll(selector)].find(e => e.offsetParent !== null);
|
||||
if (target) {
|
||||
if (!target.matches("input, select, textarea")) target.tabIndex = -1;
|
||||
target.focus();
|
||||
return;
|
||||
}
|
||||
}
|
||||
})();
|
||||
}
|
||||
window.focusFirstError = focusFirstError;
|
||||
if (document.body.dataset.errorFocus !== "manual") focusFirstError();
|
||||
</script>
|
||||
{% block script %}{% endblock %}
|
||||
</body>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue