From e11a3c57f5e70cc5a1ddb946c91e17bb1c478a75 Mon Sep 17 00:00:00 2001 From: jordi fita mas Date: Wed, 1 Mar 2023 11:55:26 +0100 Subject: [PATCH] Add validation of single tax from each class We only allow a single tax of each class in products and invoices, because it does not make sense to add two different VAT to the same product, for instance. --- pkg/form.go | 26 ++++++++++++++++++++++---- pkg/invoices.go | 1 + pkg/products.go | 1 + 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/pkg/form.go b/pkg/form.go index fe41570..e984d4e 100644 --- a/pkg/form.go +++ b/pkg/form.go @@ -127,13 +127,17 @@ func (field *SelectField) IsSelected(v string) bool { return false } -func (field *SelectField) isValidOption(selected string) bool { +func (field *SelectField) FindOption(value string) *SelectOption { for _, option := range field.Options { - if option.Value == selected { - return true + if option.Value == value { + return option } } - return false + return nil +} + +func (field *SelectField) isValidOption(selected string) bool { + return field.FindOption(selected) != nil } func MustGetOptions(ctx context.Context, conn *Conn, sql string, args ...interface{}) []*SelectOption { @@ -225,6 +229,20 @@ func (v *FormValidator) CheckValidSelectOption(field *SelectField, message strin return v.checkSelect(field, field.HasValidOptions(), message) } +func (v *FormValidator) CheckAtMostOneOfEachGroup(field *SelectField, message string) bool { + repeated := false + groups := map[string]bool{} + for _, selected := range field.Selected { + option := field.FindOption(selected) + if exists := groups[option.Group]; exists { + repeated = true + break + } + groups[option.Group] = true + } + return v.checkSelect(field, !repeated, message) +} + func (v *FormValidator) CheckValidURL(field *InputField, message string) bool { _, err := url.ParseRequestURI(field.Val) return v.checkInput(field, err == nil, message) diff --git a/pkg/invoices.go b/pkg/invoices.go index 6f2d6a3..8e24382 100644 --- a/pkg/invoices.go +++ b/pkg/invoices.go @@ -525,5 +525,6 @@ func (form *invoiceProductForm) Validate() bool { validator.CheckValidInteger(form.Discount, 0, 100, gettext("Discount must be a percentage between 0 and 100.", form.locale)) } validator.CheckValidSelectOption(form.Tax, gettext("Selected tax is not valid.", form.locale)) + validator.CheckAtMostOneOfEachGroup(form.Tax, gettext("You can only select a tax of each class.", form.locale)) return validator.AllOK() } diff --git a/pkg/products.go b/pkg/products.go index a525cab..1c6d890 100644 --- a/pkg/products.go +++ b/pkg/products.go @@ -208,5 +208,6 @@ func (form *productForm) Validate() bool { validator.CheckValidDecimal(form.Price, form.company.MinCents(), math.MaxFloat64, gettext("Price must be a number greater than zero.", form.locale)) } validator.CheckValidSelectOption(form.Tax, gettext("Selected tax is not valid.", form.locale)) + validator.CheckAtMostOneOfEachGroup(form.Tax, gettext("You can only select a tax of each class.", form.locale)) return validator.AllOK() }