-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
namespace Gt\DomValidation\Rule; | ||
|
||
use DateTime; | ||
use DOMElement; | ||
|
||
class TypeDate extends Rule { | ||
// ISO-8601 derived date formats: | ||
const FORMAT_DATE = "Y-m-d"; | ||
|
||
protected $attributes = [ | ||
"type=date", | ||
]; | ||
|
||
public function isValid(DOMElement $element, string $value):bool { | ||
if($value === "") { | ||
return true; | ||
} | ||
|
||
$dateTime = DateTime::createFromFormat( | ||
self::FORMAT_DATE, | ||
$value | ||
); | ||
return $dateTime !== false; | ||
} | ||
|
||
public function getHint(DOMElement $element, string $value):string { | ||
return "Field must be a date in the format YYYY-mm-dd"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
namespace Gt\DomValidation\Test\Rule; | ||
|
||
use Gt\DomValidation\Test\DomValidationTestCase; | ||
use Gt\DomValidation\Test\Helper\Helper; | ||
use Gt\DomValidation\ValidationException; | ||
use Gt\DomValidation\Validator; | ||
|
||
class TypeDateTest extends DomValidationTestCase { | ||
public function testTypeDate() { | ||
$form = self::getFormFromHtml(Helper::HTML_USER_PROFILE); | ||
$validator = new Validator(); | ||
|
||
$exception = null; | ||
|
||
try { | ||
$validator->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 | ||
); | ||
} | ||
} | ||
} |