From ae6ce724916217129fcaee498d7c9585ae5f5cd2 Mon Sep 17 00:00:00 2001 From: Patrik Votocek Date: Thu, 26 Mar 2015 13:03:58 +0100 Subject: [PATCH] Add short hour sanitizer --- src/Nella/Forms/DateTime/DateTimeInput.php | 14 ++++++++ .../Forms/DateTime/DateTimeInputTest.phpt | 35 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/Nella/Forms/DateTime/DateTimeInput.php b/src/Nella/Forms/DateTime/DateTimeInput.php index 5461f64..5a33782 100644 --- a/src/Nella/Forms/DateTime/DateTimeInput.php +++ b/src/Nella/Forms/DateTime/DateTimeInput.php @@ -53,6 +53,9 @@ class DateTimeInput extends \Nette\Forms\Controls\BaseControl /** @var mixed[]|array */ private $timeAttributes = array(); + /** @var bool */ + private $sanitizeShortHour = TRUE; + /** * @param string * @param string @@ -122,6 +125,12 @@ public function loadHttpData() { $this->date = $this->getHttpData(Form::DATA_LINE, '[' . static::NAME_DATE . ']'); $this->time = $this->getHttpData(Form::DATA_LINE, '[' . static::NAME_TIME . ']'); + + if ($this->sanitizeShortHour && \Nette\Utils\Strings::startsWith(\Nette\Utils\Strings::lower($this->timeFormat), 'g')) { + if (\Nette\Utils\Strings::startsWith($this->time, '00')) { + $this->time = \Nette\Utils\Strings::substring($this->time, 1); + } + } } /** @@ -226,6 +235,11 @@ public function setAttribute($name, $value = TRUE) return $this; } + public function disableShortHourSanitizer() + { + $this->sanitizeShortHour = false; + } + public static function register() { if (static::$registered) { diff --git a/tests/Nella/Forms/DateTime/DateTimeInputTest.phpt b/tests/Nella/Forms/DateTime/DateTimeInputTest.phpt index 04db883..4c1dd4d 100644 --- a/tests/Nella/Forms/DateTime/DateTimeInputTest.phpt +++ b/tests/Nella/Forms/DateTime/DateTimeInputTest.phpt @@ -332,6 +332,41 @@ class DateTimeInputTest extends \Tester\TestCase Assert::false($control->hasErrors()); } + public function testShortHourSanitizer() + { + $control = $this->createControl(array( + 'datetime' => array( + 'date' => '1978-01-23', + 'time' => '00:00', + ), + )); + + $control->addRule([$control, 'validateDateTime'], 'test'); + + $control->validate(); + + Assert::false($control->hasErrors()); + } + + public function testShortHourSanitizerDisabled() + { + $control = $this->createControl(array( + 'datetime' => array( + 'date' => '1978-01-23', + 'time' => '00:00', + ), + )); + + $control->disableShortHourSanitizer(); + $control->loadHttpData(); // this must be called + + $control->addRule([$control, 'validateDateTime'], 'test'); + + $control->validate(); + + Assert::true($control->hasErrors()); + } + /** * @throws \Nette\InvalidStateException */