Skip to content

Commit

Permalink
Implement type=date
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Dec 31, 2019
1 parent c6fd250 commit 7585654
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/DefaultValidationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -13,6 +14,7 @@ protected function setRuleList() {
new Pattern(),
new TypeNumber(),
new TypeEmail(),
new TypeDate(),
];
}
}
30 changes: 30 additions & 0 deletions src/Rule/TypeDate.php
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";
}
}
3 changes: 2 additions & 1 deletion src/Rule/TypeEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class TypeEmail extends Rule {
];

public function isValid(DOMElement $element, string $value):bool {
return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
return $value === ""
|| filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
}

public function getHint(DOMElement $element, string $value):string {
Expand Down
27 changes: 27 additions & 0 deletions test/phpunit/Helper/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,32 @@ class Helper {
</form>
HTML;

const HTML_DATE_TIME = <<<HTML
<!doctype html>
<form method="post">
<label>
<span>Starting date</span>
<input name="date" type="date" />
</label>
<label>
<span>Month of activity</span>
<input name="month" type="month" />
</label>
<label>
<span>Week of holiday</span>
<input name="week" type="week" />
</label>
<label>
<span>Time of daily reset</span>
<input name="time" type="time" />
</label>
<label>
<span>Payment date and time</span>
<input name="datetime" type="datetime-local" />
</label>
</form>
HTML;



}
45 changes: 45 additions & 0 deletions test/phpunit/Rule/TypeDateTest.php
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
);
}
}
}

0 comments on commit 7585654

Please sign in to comment.