From 044c6835d469c5a9ccb539ff9d528fdf375a3371 Mon Sep 17 00:00:00 2001 From: Sli Date: Fri, 20 Dec 2024 20:18:02 +0100 Subject: [PATCH] Properly display form errors in counter --- .../static/bundled/counter/counter-click-index.ts | 4 ++++ counter/templates/counter/counter_click.jinja | 15 ++++++++++----- counter/views/click.py | 5 +++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/counter/static/bundled/counter/counter-click-index.ts b/counter/static/bundled/counter/counter-click-index.ts index e736857d..a9294660 100644 --- a/counter/static/bundled/counter/counter-click-index.ts +++ b/counter/static/bundled/counter/counter-click-index.ts @@ -6,6 +6,7 @@ interface InitialFormData { /* Used to refill the form when the backend raises an error */ id?: string; quantity?: number; + errors?: string[]; } interface CounterConfig { @@ -28,10 +29,12 @@ interface Product { class BasketItem { quantity: number; product: Product; + errors: string[]; constructor(product: Product, quantity: number) { this.quantity = quantity; this.product = product; + this.errors = []; } getBonusQuantity(): number { @@ -59,6 +62,7 @@ exportToHtml("loadCounter", (config: CounterConfig) => { for (const entry of config.formInitial) { if (entry.id !== undefined && entry.quantity !== undefined) { this.addToBasket(entry.id, entry.quantity); + this.basket[entry.id].errors = entry.errors ?? []; } } diff --git a/counter/templates/counter/counter_click.jinja b/counter/templates/counter/counter_click.jinja index 35249b33..132f395c 100644 --- a/counter/templates/counter/counter_click.jinja +++ b/counter/templates/counter/counter_click.jinja @@ -72,20 +72,24 @@ - + {% endfor %}

{% trans %}Basket: {% endtrans %}

{% csrf_token %} - {{ form.errors }} - {{ form.non_form_errors() }}
{{ form.management_form }}