-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Fix Date\TimeType marked as invalid on request with single_text and zero seconds #20307
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
Changes from 1 commit
bf6979b
6f26d21
11f6502
4adaa2a
006f236
ea9567e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…ext and zero seconds (LuisDeimos)
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,6 +132,14 @@ public function reverseTransform($value) | |
throw new TransformationFailedException('Expected a string.'); | ||
} | ||
|
||
// handle seconds ignored by user's browser when seconds as single_text is 0 | ||
if ($this->parseFormat === 'H:i:s|') { | ||
if (!preg_match('((?:(?:[0-1][0-9])|(?:[2][0-3])|(?:[0-9])):(?:[0-5][0-9])(?::[0-5][0-9]))', $value) && | ||
preg_match('((?:(?:[0-1][0-9])|(?:[2][0-3])|(?:[0-9])):(?:[0-5][0-9]))', $value)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not only the 2nd There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the first run, and we should see only seconds, a lack of hours or minutes (without 'with_minutes' => false) itself should make a validation error it indicates an erroneous entry by the user. Here just we managed the browser omission. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
So this doesnt count for minutes and hours then? Sorry no real time to test this. But i proposed are more or less generic approach for "adding sensible defaults to the value". Ie.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that is why the condition $ this-> parseFormat === 'H: i: s |'. And, ok, these default values would apply then sent the form (within the SUBMIT events) ?, if so, would work. The ultimate goal of this is to correct the omission of the latter by the browser when the field requires seconds ('with_seconds' => true), and the user enters 0 seconds, in this case the browser sends '02: 05', in instead of '02: 05: 00 'causing a validation error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now, I fix this with an new DateTimeType implementing:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And perhaps at this point even There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, sounds great, it would be something like:
the above was to eliminate things like '99: 78 'but still has to go through validation. testing in TimeType... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of course, error finger, thanks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, he's looking good, you think? |
||
$value = $value . ":00"; | ||
} | ||
} | ||
|
||
$outputTz = new \DateTimeZone($this->outputTimezone); | ||
$dateTime = \DateTime::createFromFormat($this->parseFormat, $value, $outputTz); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,6 +167,34 @@ public function testSubmitWithSeconds() | |
$this->assertDateTimeEquals(new \DateTime('2010-06-02 03:04:05 UTC'), $form->getData()); | ||
} | ||
|
||
public function testSubmitWithSecondsAndBrowserOmissionSeconds() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps add this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test may be removed now imo. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think so, is covered by Time Type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok unit test only in TimeType ;) |
||
{ | ||
$form = $this->factory->create('datetime', null, array( | ||
'model_timezone' => 'UTC', | ||
'view_timezone' => 'UTC', | ||
'date_widget' => 'choice', | ||
'years' => array(2010), | ||
'time_widget' => 'single_text', | ||
'input' => 'datetime', | ||
'with_seconds' => true, | ||
)); | ||
|
||
$form->setData(new \DateTime()); | ||
|
||
$input = array( | ||
'date' => array( | ||
'day' => '2', | ||
'month' => '6', | ||
'year' => '2010', | ||
), | ||
'time' => '03:04' | ||
); | ||
|
||
$form->submit($input); | ||
|
||
$this->assertDateTimeEquals(new \DateTime('2010-06-02 03:04:00 UTC'), $form->getData()); | ||
} | ||
|
||
public function testSubmitDifferentTimezones() | ||
{ | ||
$form = $this->factory->create('datetime', null, array( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should also work without the pipe character set.