Add shorter format datetime-local defaults#761
Conversation
Allows validation of short datetime-local formats (%Y-%m-%dT%H:%M) by default, as it the default in common browsers (where step size is unchanged). Relates to pallets-eco#450
|
Thank you for your contribution. |
|
Thank you. The style test is still failing though. |
|
That should make the tox style testing pass now. It seems that black wanted to re-format that line. I've also seen that two tests are failing from test_datetime.py. Would you like me to look at those before merge? |
|
The GHA tests look OK. Are you seeing issues with |
|
Two tests failing when invoking I've just re-run the tests without the changes from this pull request and I'm still seeing the same failures, so I imagine that it doesn't have anything to do with this commit.
__________________________________________________________________ test_basic ___________________________________________________________________
def test_basic():
d = datetime(2008, 5, 5, 4, 30, 0, 0)
# Basic test with both inputs
form = F(
DummyPostData(
a=["2008-05-05", "04:30:00"], b=["2008-05-05 04:30"], c=["5/5/2008 4:30"]
)
)
assert form.a.data == d
assert (
form.a()
== """<input id="a" name="a" type="datetime" value="2008-05-05 04:30:00">"""
)
assert form.b.data == d
assert (
form.b()
== """<input id="b" name="b" type="datetime" value="2008-05-05 04:30">"""
)
assert form.c.data == d
assert (
form.c() == """<input id="c" name="c" type="datetime" value="5/5/2008 4:30">"""
)
assert form.validate()
# Test with a missing input
form = F(DummyPostData(a=["2008-05-05"]))
assert not form.validate()
assert form.a.errors[0] == "Not a valid datetime value."
form = F(a=d, b=d, c=d)
assert form.validate()
assert form.a._value() == "2008-05-05 04:30:00"
assert form.b._value() == "2008-05-05 04:30"
> assert form.c._value() == "5/5/2008 4:30"
tests\fields\test_datetime.py:52:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <wtforms.fields.datetime.DateTimeField object at 0x0000018C16BD84D0>
def _value(self):
if self.raw_data:
return " ".join(self.raw_data)
> return self.data and self.data.strftime(self.format[0]) or ""
E ValueError: Invalid format string
src\wtforms\fields\datetime.py:36: ValueError
__________________________________________________________________ test_basic ___________________________________________________________________
def test_basic():
d = datetime(2008, 5, 5, 4, 30, 0, 0)
# Basic test with both inputs
form = F(
DummyPostData(
a=["2008-05-05", "04:30:00"], b=["2008-05-05 04:30"], c=["5/5/2008 4:30"]
)
)
assert form.a.data == d
assert (
form.a()
== '<input id="a" name="a" type="datetime-local" value="2008-05-05 04:30:00">'
)
assert form.b.data == d
assert (
form.b()
== '<input id="b" name="b" type="datetime-local" value="2008-05-05 04:30">'
)
assert form.c.data == d
assert (
form.c()
== '<input id="c" name="c" type="datetime-local" value="5/5/2008 4:30">'
)
assert form.validate()
assert form.validate()
assert form.a._value() == "2008-05-05 04:30:00"
assert form.b._value() == "2008-05-05 04:30"
> assert form.c._value() == "5/5/2008 4:30"
tests\fields\test_datetimelocal.py:53:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <wtforms.fields.datetime.DateTimeLocalField object at 0x0000018C16A64250>
def _value(self): if self.raw_data:
return " ".join(self.raw_data)
> return self.data and self.data.strftime(self.format[0]) or ""
E ValueError: Invalid format string
src\wtforms\fields\datetime.py:36: ValueError |
Describe the issue you are attempting to fix
This issue was previously raised in #450.
The HTML standard for datetime-local inputs set the default step attribute to 60, meaning that the lowest denomination of input can be expressed in minutes, not seconds.
https://html.spec.whatwg.org/#local-date-and-time-state-(type=datetime-local)
In such cases, it seems that common browser behaviour is to then send the input value, without any seconds, on form submission, in the format -
The current default of WTForms is to validate against two possible date formats, both that require seconds -
If seconds are not found, the validation will fail -
Current fixes
This issue can be fixed by adding the correct format to a DateTimeLocalField() instance -
or by change input step size to allow seconds (in HTML)**
Proposal
Very simple PR to bring WTForms in line with the default behaviour of some browsers, by adding two formats (without seconds) to the default formats of DateTimeLocalField.