1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 08:56:07 +00:00

fix: a rejected form marks only its fields in error

django-bootstrap5 marks every error-free field of a sent form green
(is-valid), so a form that was refused and not saved looked approved
beside its red message. A field renderer of the panel's own marks only
the fields in error; the others look as they did before the form was
sent. Set in settings.py.template (BOOTSTRAP5['field_renderers']).
This commit is contained in:
catborise 2026-11-18 07:53:46 +03:00
parent df11685037
commit 558eec527c
3 changed files with 41 additions and 0 deletions

12
webvirtcloud/bootstrap.py Normal file
View file

@ -0,0 +1,12 @@
"""django-bootstrap5's field renderer, marking only the fields in error."""
from django_bootstrap5.renderers import FieldRenderer as BootstrapFieldRenderer
class FieldRenderer(BootstrapFieldRenderer):
"""A sent form marks its fields in error red and leaves the others as
they were: a green "valid" mark on a form that was not saved reads as if
it had been."""
def get_server_side_validation_classes(self):
return "is-invalid" if self.field_errors else ""

View file

@ -246,6 +246,9 @@ USE_I18N = True
USE_TZ = True
# a sent form marks only its fields in error (django-bootstrap5 marks the others green)
BOOTSTRAP5 = {"field_renderers": {"default": "webvirtcloud.bootstrap.FieldRenderer"}}
STATIC_URL = "/static/"
# Source assets live in static/; collectstatic gathers them with the apps'

View file

@ -0,0 +1,26 @@
"""A rejected form marks its fields in error, not the others: a green
"valid" mark beside a form that was not saved reads as if it were."""
from django import forms
from django.template import Context, Template
from django.test import SimpleTestCase
class ExampleForm(forms.Form):
name = forms.CharField()
size = forms.IntegerField()
class FormMarksTestCase(SimpleTestCase):
def render(self, form):
return Template("{% load django_bootstrap5 %}{% bootstrap_form form %}").render(Context({"form": form}))
def test_only_fields_in_error_are_marked(self):
html = self.render(ExampleForm({"name": "ok", "size": "x"}))
self.assertIn("is-invalid", html)
self.assertNotIn("is-valid", html.replace("is-invalid", ""))
def test_a_form_not_sent_is_not_marked(self):
html = self.render(ExampleForm())
self.assertNotIn("is-invalid", html)
self.assertNotIn("is-valid", html)