Skip to content
This repository was archived by the owner on Nov 15, 2020. It is now read-only.

Commit ae6ce72

Browse files
committed
Add short hour sanitizer
1 parent f771b4b commit ae6ce72

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

src/Nella/Forms/DateTime/DateTimeInput.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class DateTimeInput extends \Nette\Forms\Controls\BaseControl
5353
/** @var mixed[]|array */
5454
private $timeAttributes = array();
5555

56+
/** @var bool */
57+
private $sanitizeShortHour = TRUE;
58+
5659
/**
5760
* @param string
5861
* @param string
@@ -122,6 +125,12 @@ public function loadHttpData()
122125
{
123126
$this->date = $this->getHttpData(Form::DATA_LINE, '[' . static::NAME_DATE . ']');
124127
$this->time = $this->getHttpData(Form::DATA_LINE, '[' . static::NAME_TIME . ']');
128+
129+
if ($this->sanitizeShortHour && \Nette\Utils\Strings::startsWith(\Nette\Utils\Strings::lower($this->timeFormat), 'g')) {
130+
if (\Nette\Utils\Strings::startsWith($this->time, '00')) {
131+
$this->time = \Nette\Utils\Strings::substring($this->time, 1);
132+
}
133+
}
125134
}
126135

127136
/**
@@ -226,6 +235,11 @@ public function setAttribute($name, $value = TRUE)
226235
return $this;
227236
}
228237

238+
public function disableShortHourSanitizer()
239+
{
240+
$this->sanitizeShortHour = false;
241+
}
242+
229243
public static function register()
230244
{
231245
if (static::$registered) {

tests/Nella/Forms/DateTime/DateTimeInputTest.phpt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,41 @@ class DateTimeInputTest extends \Tester\TestCase
332332
Assert::false($control->hasErrors());
333333
}
334334

335+
public function testShortHourSanitizer()
336+
{
337+
$control = $this->createControl(array(
338+
'datetime' => array(
339+
'date' => '1978-01-23',
340+
'time' => '00:00',
341+
),
342+
));
343+
344+
$control->addRule([$control, 'validateDateTime'], 'test');
345+
346+
$control->validate();
347+
348+
Assert::false($control->hasErrors());
349+
}
350+
351+
public function testShortHourSanitizerDisabled()
352+
{
353+
$control = $this->createControl(array(
354+
'datetime' => array(
355+
'date' => '1978-01-23',
356+
'time' => '00:00',
357+
),
358+
));
359+
360+
$control->disableShortHourSanitizer();
361+
$control->loadHttpData(); // this must be called
362+
363+
$control->addRule([$control, 'validateDateTime'], 'test');
364+
365+
$control->validate();
366+
367+
Assert::true($control->hasErrors());
368+
}
369+
335370
/**
336371
* @throws \Nette\InvalidStateException
337372
*/

0 commit comments

Comments
 (0)