Skip to content

Commit

Permalink
Clean and round decimals for turnout_percentage
Browse files Browse the repository at this point in the history
This change creates a new widget class to
offer users the ability to copy and past
input with decimals without the additional
step of reformatting and rounding.

When we clean in the onchange attribute,
we also address form errors that have caused
users to have to reload the page to enter
results
  • Loading branch information
VirginiaDooley committed Jul 18, 2024
1 parent 4e9e751 commit 3422e69
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
4 changes: 2 additions & 2 deletions ynr/apps/uk_results/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.db.models.functions import Coalesce
from uk_results.helpers import RecordBallotResultsHelper
from utils.db import LastWord, NullIfBlank
from utils.widgets import DCIntegerInput
from utils.widgets import DCIntegerInput, DCPercentageInput

from .models import CandidateResult, ResultSet

Expand All @@ -34,7 +34,7 @@ class Meta:
}
),
"num_turnout_reported": DCIntegerInput(),
"turnout_percentage": DCIntegerInput(),
"turnout_percentage": DCPercentageInput(),
"num_spoilt_ballots": DCIntegerInput(),
"total_electorate": DCIntegerInput(),
}
Expand Down
3 changes: 1 addition & 2 deletions ynr/apps/utils/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ def build_attrs(self, base_attrs, extra_attrs=None):
{
"pattern": r"[0-9\s\.]*",
"oninvalid": "this.setCustomValidity('Enter a percentage or a whole number')",
# round to the nearest whole number and remove the decimal
"onchange": "this.value = Math.round(this.value).toString().replace(/,/g, '')",
"onchange": "this.value = Math.round(this.value.replace(/\D/g, '')).toString()",
}
)
return attrs

0 comments on commit 3422e69

Please sign in to comment.