mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-11 09:28:27 -04:00
Add validation for provider SMS percentages
We could alternatively put the "add up to 100%" error on the page
using form-level errors [^1] and a custom flash message. Putting
the error on each field is slightly simpler and does make it clear
the issue is with all of the fields together.
[^1]: 22636b55ed
This commit is contained in:
@@ -1742,6 +1742,8 @@ class EstimateUsageForm(StripWhitespaceForm):
|
||||
|
||||
class AdminProviderRatioForm(Form):
|
||||
def __init__(self, providers):
|
||||
self._providers = providers
|
||||
|
||||
# hack: https://github.com/wtforms/wtforms/issues/736
|
||||
self._unbound_fields = [
|
||||
(
|
||||
@@ -1760,6 +1762,25 @@ class AdminProviderRatioForm(Form):
|
||||
for provider in providers
|
||||
})
|
||||
|
||||
def validate(self):
|
||||
if not super().validate():
|
||||
return False
|
||||
|
||||
total = sum(
|
||||
getattr(self, provider['identifier']).data
|
||||
for provider in self._providers
|
||||
)
|
||||
|
||||
if total == 100:
|
||||
return True
|
||||
|
||||
for provider in self._providers:
|
||||
getattr(self, provider['identifier']).errors += [
|
||||
'Must add up to 100%'
|
||||
]
|
||||
|
||||
return False
|
||||
|
||||
|
||||
class ServiceContactDetailsForm(StripWhitespaceForm):
|
||||
contact_details_type = RadioField(
|
||||
|
||||
Reference in New Issue
Block a user