-
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.
* Implement required select field * Implement <select> option matching, closes #3
- Loading branch information
Showing
6 changed files
with
219 additions
and
3 deletions.
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
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,44 @@ | ||
<?php | ||
namespace Gt\DomValidation\Rule; | ||
|
||
use DOMElement; | ||
|
||
class SelectElement extends Rule { | ||
public function isValid(DOMElement $element, string $value):bool { | ||
$availableValues = []; | ||
|
||
if($element->tagName !== "select") { | ||
return true; | ||
} | ||
|
||
if($value === "") { | ||
return true; | ||
} | ||
|
||
$optionElementList = $element->getElementsByTagName("option"); | ||
for($i = 0, $len = $optionElementList->length; $i < $len; $i++) { | ||
$option = $optionElementList->item($i); | ||
|
||
if($option->hasAttribute("value")) { | ||
$optionValue = $option->getAttribute("value"); | ||
} | ||
else { | ||
$optionValue = $option->nodeValue; | ||
} | ||
|
||
if($optionValue !== "") { | ||
$availableValues []= $optionValue; | ||
} | ||
} | ||
|
||
if(!in_array($value, $availableValues)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function getHint(DOMElement $element, string $value):string { | ||
return "This field's value must match one of the available options"; | ||
} | ||
} |
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,136 @@ | ||
<?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 SelectElementTest extends DomValidationTestCase { | ||
public function testSelect() { | ||
$form = self::getFormFromHtml(Helper::HTML_SELECT); | ||
$validator = new Validator(); | ||
|
||
$exception = null; | ||
|
||
try { | ||
$validator->validate($form, [ | ||
"currency" => "GBP", | ||
]); | ||
} | ||
catch(ValidationException $exception) {} | ||
|
||
self::assertNull($exception); | ||
} | ||
|
||
public function testSelectMissingRequired() { | ||
$form = self::getFormFromHtml(Helper::HTML_SELECT); | ||
$validator = new Validator(); | ||
|
||
try { | ||
$validator->validate($form, [ | ||
"currency" => "", | ||
]); | ||
} | ||
catch(ValidationException $exception) { | ||
$errorArray = iterator_to_array($validator->getLastErrorList()); | ||
self::assertCount(1, $errorArray); | ||
$currencyErrorArray = $errorArray["currency"]; | ||
self::assertContains( | ||
"This field is required", | ||
$currencyErrorArray | ||
); | ||
} | ||
} | ||
|
||
public function testSelectTextContent() { | ||
$form = self::getFormFromHtml(Helper::HTML_SELECT); | ||
$validator = new Validator(); | ||
|
||
$exception = null; | ||
|
||
try { | ||
$validator->validate($form, [ | ||
"currency" => "USD", | ||
"sort" => "Descending", | ||
]); | ||
} | ||
catch(ValidationException $exception) {} | ||
|
||
self::assertNull($exception); | ||
} | ||
|
||
public function testSelectTextContentInvalid() { | ||
$form = self::getFormFromHtml(Helper::HTML_SELECT); | ||
$validator = new Validator(); | ||
|
||
try { | ||
$validator->validate($form, [ | ||
"currency" => "USD", | ||
"sort" => "Random", // This <option> does not exist | ||
]); | ||
} | ||
catch(ValidationException $exception) { | ||
$errorArray = iterator_to_array($validator->getLastErrorList()); | ||
self::assertCount(1, $errorArray); | ||
$currencyErrorArray = $errorArray["sort"]; | ||
self::assertContains( | ||
"This field's value must match one of the available options", | ||
$currencyErrorArray | ||
); | ||
} | ||
} | ||
|
||
public function testSelectValueInvalid() { | ||
$form = self::getFormFromHtml(Helper::HTML_SELECT); | ||
$validator = new Validator(); | ||
|
||
try { | ||
$validator->validate($form, [ | ||
"currency" => "USD", | ||
"connections" => "All", // This is invalid | ||
// There is an <option> with "All" as its text content, but not with this value. | ||
]); | ||
} | ||
catch(ValidationException $exception) { | ||
$errorArray = iterator_to_array($validator->getLastErrorList()); | ||
self::assertCount(1, $errorArray); | ||
$currencyErrorArray = $errorArray["connections"]; | ||
self::assertContains( | ||
"This field's value must match one of the available options", | ||
$currencyErrorArray | ||
); | ||
} | ||
} | ||
|
||
public function testSelectTwoInvalidOptionsAndOneMissing() { | ||
$form = self::getFormFromHtml(Helper::HTML_SELECT); | ||
$validator = new Validator(); | ||
|
||
try { | ||
$validator->validate($form, [ | ||
"connections" => "none", | ||
"sort" => "random", | ||
]); | ||
} | ||
catch(ValidationException $exception) { | ||
$errorArray = iterator_to_array($validator->getLastErrorList()); | ||
self::assertCount(3, $errorArray); | ||
$currencyErrorArray = $errorArray["currency"]; | ||
$sortErrorArray = $errorArray["sort"]; | ||
$connectionsErrorArray = $errorArray["connections"]; | ||
self::assertContains( | ||
"This field is required", | ||
$currencyErrorArray | ||
); | ||
self::assertContains( | ||
"This field's value must match one of the available options", | ||
$sortErrorArray | ||
); | ||
self::assertContains( | ||
"This field's value must match one of the available options", | ||
$connectionsErrorArray | ||
); | ||
} | ||
} | ||
} |