From fb9ff1d7ff21fe7a25f23a13d9be8963a102f6d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Tue, 7 Feb 2023 09:30:06 +0800 Subject: [PATCH] Added to validate if the base account is available in the AccountForm form with the BaseAccountAvailable validator. --- src/accounting/account/forms.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/accounting/account/forms.py b/src/accounting/account/forms.py index 355f99e..52accbd 100644 --- a/src/accounting/account/forms.py +++ b/src/accounting/account/forms.py @@ -42,13 +42,25 @@ class BaseAccountExists: "The base account does not exist.")) +class BaseAccountAvailable: + """The validator to check if the base account is available.""" + + def __call__(self, form: FlaskForm, field: StringField) -> None: + if field.data == "": + return + if len(field.data) != 4: + raise ValidationError(lazy_gettext( + "The base account is not available.")) + + class AccountForm(FlaskForm): """The form to create or edit an account.""" base_code = StringField( filters=[strip_text], validators=[ DataRequired(lazy_gettext("Please select the base account.")), - BaseAccountExists()]) + BaseAccountExists(), + BaseAccountAvailable()]) """The code of the base account.""" title = StringField( filters=[strip_text],