Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GovDateInput should drop leading zeros #85

Open
markhobson opened this issue Feb 5, 2024 · 1 comment
Open

GovDateInput should drop leading zeros #85

markhobson opened this issue Feb 5, 2024 · 1 comment

Comments

@markhobson
Copy link

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant