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.
This commit is contained in:
jordi fita mas 2023-03-01 11:55:26 +01:00
parent 6a8ebab686
commit e11a3c57f5
3 changed files with 24 additions and 4 deletions

View File

@ -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)

View File

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

View File

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