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

Commit

Permalink
Add short hour sanitizer
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrtak-CZ committed Mar 26, 2015
1 parent f771b4b commit ae6ce72
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Nella/Forms/DateTime/DateTimeInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
35 changes: 35 additions & 0 deletions tests/Nella/Forms/DateTime/DateTimeInputTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit ae6ce72

Please sign in to comment.