govuk-frontend-wtf icon indicating copy to clipboard operation
govuk-frontend-wtf copied to clipboard

GovDateInput should drop leading zeros

Open markhobson opened this issue 1 year ago • 1 comments
trafficstars

Describe the bug The GovDateInput widget renders the day and month fields with leading zeros, for example 02 03 2007. This is contrary to what is recommended by the dates pattern which is to omit leading zeros.

To Reproduce

>>> from datetime import date
>>> from wtforms import DateField, Form
>>> from govuk_frontend_wtf.wtforms_widgets import GovDateInput
>>> class MyForm(Form):
...     date = DateField(widget=GovDateInput())
>>> form = MyForm(data={"date": date(2007, 3, 2)})
>>> form.date.widget.map_gov_params(form.date, id="date")
{'id': 'date', 'name': 'date', 'label': {'text': 'Date'}, 'attributes': {}, 'hint': {'text': ''}, 'fieldset': {'legend': {'text': 'Date'}}, 'items': [{'label': 'Day', 'id': 'date-day', 'name': 'date', 'classes': 'govuk-input--width-2', 'value': '02'}, {'label': 'Month', 'id': 'date-month', 'name': 'date', 'classes': 'govuk-input--width-2', 'value': '03'}, {'label': 'Year', 'id': 'date-year', 'name': 'date', 'classes': 'govuk-input--width-4', 'value': '2007'}]}

Or formatted:

{
    'id': 'date',
    'name': 'date',
    'label': {'text': 'Date'},
    'attributes': {},
    'hint': {'text': ''},
    'fieldset': {'legend': {'text': 'Date'}},
    'items': [
        {'label': 'Day', 'id': 'date-day', 'name': 'date', 'classes': 'govuk-input--width-2', 'value': '02'},
        {'label': 'Month', 'id': 'date-month', 'name': 'date', 'classes': 'govuk-input--width-2', 'value': '03'},
        {'label': 'Year', 'id': 'date-year', 'name': 'date', 'classes': 'govuk-input--width-4', 'value': '2007'},
    ],
}

Expected behavior I would expect the day and month field values not to have leading zeros:

{
    ...
    'items': [
        {'label': 'Day', 'id': 'date-day', 'name': 'date', 'classes': 'govuk-input--width-2', 'value': '2'},
        {'label': 'Month', 'id': 'date-month', 'name': 'date', 'classes': 'govuk-input--width-2', 'value': '3'},
        {'label': 'Year', 'id': 'date-year', 'name': 'date', 'classes': 'govuk-input--width-4', 'value': '2007'},
    ],
}

This behaviour is due to the format used in GovDateInput: %d %m %Y. To omit leading zeros we can use %-d %-m %Y, although this is platform dependent.

markhobson avatar Feb 05 '24 17:02 markhobson

This issue can be worked around with the following custom widget:

class RemoveLeadingZerosGovDateInput(GovDateInput):  # type: ignore
    """
    A GOV.UK date input widget that removes leading zeros from day and month fields.
    """

    def map_gov_params(self, field: Field, **kwargs: str) -> dict[str, Any]:
        params: dict[str, Any] = super().map_gov_params(field, **kwargs)

        if field.raw_data is None and field.data:
            day, month = field.data.strftime("%-d %-m").split(" ")
            params["items"][0]["value"] = day
            params["items"][1]["value"] = month

        return params

markhobson avatar Feb 12 '24 12:02 markhobson