From 7585654adc925e7a63fad8e31f67e08eba73b8c1 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Tue, 31 Dec 2019 14:19:37 +0000 Subject: [PATCH] Implement type=date --- src/DefaultValidationRules.php | 2 ++ src/Rule/TypeDate.php | 30 ++++++++++++++++++++ src/Rule/TypeEmail.php | 3 +- test/phpunit/Helper/Helper.php | 27 ++++++++++++++++++ test/phpunit/Rule/TypeDateTest.php | 45 ++++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 src/Rule/TypeDate.php create mode 100644 test/phpunit/Rule/TypeDateTest.php diff --git a/src/DefaultValidationRules.php b/src/DefaultValidationRules.php index 24afe47..f2e6285 100644 --- a/src/DefaultValidationRules.php +++ b/src/DefaultValidationRules.php @@ -3,6 +3,7 @@ use Gt\DomValidation\Rule\Pattern; use Gt\DomValidation\Rule\Required; +use Gt\DomValidation\Rule\TypeDate; use Gt\DomValidation\Rule\TypeEmail; use Gt\DomValidation\Rule\TypeNumber; @@ -13,6 +14,7 @@ protected function setRuleList() { new Pattern(), new TypeNumber(), new TypeEmail(), + new TypeDate(), ]; } } \ No newline at end of file diff --git a/src/Rule/TypeDate.php b/src/Rule/TypeDate.php new file mode 100644 index 0000000..6eed18e --- /dev/null +++ b/src/Rule/TypeDate.php @@ -0,0 +1,30 @@ + HTML; + const HTML_DATE_TIME = << +
+ + + + + +
+HTML; + + } \ No newline at end of file diff --git a/test/phpunit/Rule/TypeDateTest.php b/test/phpunit/Rule/TypeDateTest.php new file mode 100644 index 0000000..bac7659 --- /dev/null +++ b/test/phpunit/Rule/TypeDateTest.php @@ -0,0 +1,45 @@ +validate($form, [ + "dob" => "1968-11-22", + ]); + } + catch(ValidationException $exception) {} + + self::assertNull($exception); + } + + public function testTypeDateInvalid() { + $form = self::getFormFromHtml(Helper::HTML_USER_PROFILE); + $validator = new Validator(); + + try { + $validator->validate($form, [ + "dob" => "November 22nd 1968", + ]); + } + catch(ValidationException $exception) { + $errorArray = iterator_to_array($validator->getLastErrorList()); + self::assertCount(1, $errorArray); + $dobErrorArray = $errorArray["dob"]; + self::assertContains( + "Field must be a date in the format YYYY-mm-dd", + $dobErrorArray + ); + } + } +} \ No newline at end of file