Skip to content

Commit

Permalink
Implement final type, type=time
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Jan 1, 2020
1 parent 0cd4078 commit 6a1f3d7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Rule/TypeDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ class TypeDate extends Rule {
const FORMAT_MONTH = "Y-m";
const FORMAT_WEEK = "Y-\WW";
const FORMAT_DATETIME_LOCAL = "Y-m-d\TH:i";
const FORMAT_TIME = "H:i";

protected $attributes = [
"type=date",
"type=month",
"type=week",
"type=datetime-local",
"type=time",
];

public function isValid(DOMElement $element, string $value):bool {
Expand Down Expand Up @@ -65,6 +67,13 @@ public function isValid(DOMElement $element, string $value):bool {
$value
);
break;

case "time":
$dateTime = DateTime::createFromFormat(
self::FORMAT_TIME,
$value
);
break;
}

return $dateTime !== false;
Expand All @@ -90,6 +99,10 @@ public function getHint(DOMElement $element, string $value):string {
case "datetime-local":
$format = self::FORMAT_DATETIME_LOCAL;
break;

case "time":
$format = self::FORMAT_TIME;
break;
}

return "Field must be a $type in the format $format";
Expand Down
36 changes: 36 additions & 0 deletions test/phpunit/Rule/TypeDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,40 @@ public function testTypeDatetimeLocalInvalid() {
);
}
}

public function testTypeTime() {
$form = self::getFormFromHtml(Helper::HTML_DATE_TIME);
$validator = new Validator();

$exception = null;

try {
$validator->validate($form, [
"time" => "15:37",
]);
}
catch(ValidationException $exception) {}

self::assertNull($exception);
}

public function testTypeTimeInvalid() {
$form = self::getFormFromHtml(Helper::HTML_DATE_TIME);
$validator = new Validator();

try {
$validator->validate($form, [
"time" => "3:37pm",
]);
}
catch(ValidationException $exception) {
$errorArray = iterator_to_array($validator->getLastErrorList());
self::assertCount(1, $errorArray);
$timeErrorArray = $errorArray["time"];
self::assertContains(
"Field must be a time in the format H:i",
$timeErrorArray
);
}
}
}

0 comments on commit 6a1f3d7

Please sign in to comment.