From dba41f97f3a0f803949140f23029b6aa5ccab88e Mon Sep 17 00:00:00 2001 From: catborise Date: Mon, 19 Oct 2026 23:13:25 +0300 Subject: [PATCH] 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. --- instances/templates/create_instance_w2.html | 393 ++++++++++++------ .../templates/instances/form_errors.html | 9 + instances/templatetags/form_fields.py | 35 ++ instances/tests/create_page.py | 29 ++ instances/tests/test_create_form_input.py | 181 ++++++++ instances/tests/test_create_page_js.py | 53 ++- instances/views.py | 37 ++ templates/base.html | 30 +- webvirtcloud/tests/test_error_focus.py | 37 +- 9 files changed, 640 insertions(+), 164 deletions(-) create mode 100644 instances/templates/instances/form_errors.html create mode 100644 instances/templatetags/form_fields.py create mode 100644 instances/tests/create_page.py create mode 100644 instances/tests/test_create_form_input.py diff --git a/instances/templates/create_instance_w2.html b/instances/templates/create_instance_w2.html index fdd42491..1b420428 100644 --- a/instances/templates/create_instance_w2.html +++ b/instances/templates/create_instance_w2.html @@ -2,6 +2,9 @@ {% load i18n %} {% load static %} {% load operations %} +{% load django_bootstrap5 %} +{% load form_fields %} +{% block body_attrs %} data-error-focus="manual" data-submitted-form="{{ submitted_form }}"{% endblock %} {% load bootstrap_icons %} {% block title %}{% trans "Create new instance" %}{% endblock %} @@ -20,20 +23,6 @@ {% block content %} {% include 'pleasewaitdialog.html' %} - {% if form.errors %} - {% for field in form %} - {% for error in field.errors %} -
- {{ field.label }}: {{ error|escape }} -
- {% endfor %} - {% endfor %} - {% for error in form.non_field_errors %} -
- {{ error|escape }} -
- {% endfor %} - {% endif %}
@@ -86,7 +75,7 @@ - {% for flavor in flavors %} + {% for flavor, form_id, vm_form in flavor_forms %} {{ forloop.counter }} {{ flavor.label }} @@ -94,7 +83,7 @@ {{ flavor.memory }} {% trans "MB" %} {{ flavor.disk }} {% trans "GB" %} -