Added the all_errors pseudo property to the RecurringItemForm form, and applied it to the form-recurring-item.html template.

This commit is contained in:
依瑪貓 2023-03-22 19:36:41 +08:00
parent 44ac53f15c
commit 8f477dd6f1
2 changed files with 15 additions and 2 deletions

View File

@ -20,6 +20,7 @@
import re import re
from flask import render_template from flask import render_template
from flask_babel import LazyString
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from wtforms import StringField, FieldList, FormField, IntegerField from wtforms import StringField, FieldList, FormField, IntegerField
from wtforms.validators import DataRequired, ValidationError from wtforms.validators import DataRequired, ValidationError
@ -119,6 +120,18 @@ class RecurringItemForm(FlaskForm):
account: Account | None = Account.find_by_code(self.account_code.data) account: Account | None = Account.find_by_code(self.account_code.data)
return None if account is None else str(account) return None if account is None else str(account)
@property
def all_errors(self) -> list[str | LazyString]:
"""Returns all the errors of the form.
:return: All the errors of the form.
"""
all_errors: list[str | LazyString] = []
for key in self.errors:
if key != "csrf_token":
all_errors.extend(self.errors[key])
return all_errors
class RecurringExpenseForm(RecurringItemForm): class RecurringExpenseForm(RecurringItemForm):
"""The sub-form to add or update the recurring expenses.""" """The sub-form to add or update the recurring expenses."""

View File

@ -27,12 +27,12 @@ First written: 2023/3/22
<input id="accounting-recurring-{{ expense_income }}-{{ item_index }}-description-template" type="hidden" name="recurring-{{ expense_income }}-{{ item_index }}-description_template" value="{{ form.description_template.data|accounting_default }}"> <input id="accounting-recurring-{{ expense_income }}-{{ item_index }}-description-template" type="hidden" name="recurring-{{ expense_income }}-{{ item_index }}-description_template" value="{{ form.description_template.data|accounting_default }}">
<div class="d-flex justify-content-between"> <div class="d-flex justify-content-between">
<div id="accounting-recurring-{{ expense_income }}-{{ item_index }}-content" class="w-100"> <div id="accounting-recurring-{{ expense_income }}-{{ item_index }}-content" class="w-100">
<div id="accounting-recurring-{{ expense_income }}-{{ item_index }}-control" class="form-control accounting-clickable {% if form.form_errors %} is-invalid {% endif %}" data-bs-toggle="modal" data-bs-target="#accounting-recurring-item-editor-{{ expense_income }}-modal"> <div id="accounting-recurring-{{ expense_income }}-{{ item_index }}-control" class="form-control accounting-clickable {% if form.all_errors %} is-invalid {% endif %}" data-bs-toggle="modal" data-bs-target="#accounting-recurring-item-editor-{{ expense_income }}-modal">
<div id="accounting-recurring-{{ expense_income }}-{{ item_index }}-account-text" class="small">{{ form.account_text|accounting_default }}</div> <div id="accounting-recurring-{{ expense_income }}-{{ item_index }}-account-text" class="small">{{ form.account_text|accounting_default }}</div>
<div id="accounting-recurring-{{ expense_income }}-{{ item_index }}-name-text">{{ form.name.data|accounting_default }}</div> <div id="accounting-recurring-{{ expense_income }}-{{ item_index }}-name-text">{{ form.name.data|accounting_default }}</div>
<div id="accounting-recurring-{{ expense_income }}-{{ item_index }}-description-template-text" class="small">{{ form.description_template.data|accounting_default }}</div> <div id="accounting-recurring-{{ expense_income }}-{{ item_index }}-description-template-text" class="small">{{ form.description_template.data|accounting_default }}</div>
</div> </div>
<div id="accounting-recurring-{{ expense_income }}-{{ item_index }}-error" class="invalid-feedback">{% if form.form_errors %}{{ form.form_errors[0] }}{% endif %}</div> <div id="accounting-recurring-{{ expense_income }}-{{ item_index }}-error" class="invalid-feedback">{% if form.all_errors %}{{ form.all_errors[0] }}{% endif %}</div>
</div> </div>
<div class="ms-2"> <div class="ms-2">