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() }