-
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.
* ci: scrutinizer simplification * tweak: grammar fix * feature: allow multiple values like checkboxes closes #22 * build: install with PHP 8.1 * tweak: test correct checkable
- Loading branch information
Showing
21 changed files
with
588 additions
and
296 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,85 @@ | ||
<?php | ||
/** | ||
* To run this example, from a terminal navigate to the example directory and run: | ||
* php -S 0.0.0.0:8080 | ||
* then visit http://localhost:8080/02-checkboxes.php in your web browser. | ||
* | ||
* The purpose of this example is to show how an invalid state occurs when | ||
* the user hacks the client-side to allow submitting a field that doesn't exist | ||
* in the source HTML. | ||
*/ | ||
|
||
use Gt\Dom\HTMLDocument; | ||
use Gt\DomValidation\ValidationException; | ||
use Gt\DomValidation\Validator; | ||
|
||
require __DIR__ . "/../vendor/autoload.php"; | ||
|
||
$html = <<<HTML | ||
<!doctype html> | ||
<style> | ||
[data-validation-error] { | ||
border-left: 2px solid red; | ||
} | ||
[data-validation-error]::before { | ||
content: attr(data-validation-error); | ||
color: red; | ||
font-weight: bold; | ||
} | ||
label { | ||
display: block; | ||
padding: 1rem; | ||
} | ||
label span { | ||
display: block; | ||
} | ||
</style> | ||
<!doctype html> | ||
<form method="post"> | ||
<fieldset> | ||
<legend>Available currencies</legend> | ||
<label> | ||
<input type="checkbox" name="currency[]" value="GBP" /> | ||
<span>£ Pound (GBP)</span> | ||
</label> | ||
<label> | ||
<input type="checkbox" name="currency[]" value="USD" /> | ||
<span>$ Dollar (USD)</span> | ||
</label> | ||
<label> | ||
<input type="checkbox" name="currency[]" value="EUR" /> | ||
<span>€ Pound (EUR)</span> | ||
</label> | ||
</fieldset> | ||
<button name="do" value="submit">Submit</button> | ||
</form> | ||
HTML; | ||
|
||
function example(HTMLDocument $document, array $input) { | ||
$validator = new Validator(); | ||
$form = $document->forms[0]; | ||
|
||
try { | ||
$validator->validate($form, $input); | ||
} | ||
catch(ValidationException $exception) { | ||
foreach($validator->getLastErrorList() as $name => $message) { | ||
$errorElement = $form->querySelector("[name=$name]"); | ||
$errorElement->parentNode->dataset->validationError = $message; | ||
} | ||
return; | ||
} | ||
|
||
echo "Currencies selected: "; | ||
echo implode(", ", $input["currency"]); | ||
exit; | ||
} | ||
|
||
$document = new HTMLDocument($html); | ||
|
||
if(isset($_POST["do"]) && $_POST["do"] === "submit") { | ||
example($document, $_POST); | ||
} | ||
|
||
echo $document; |
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
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
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,40 @@ | ||
<?php | ||
namespace Gt\DomValidation\Rule\Trait; | ||
|
||
use Gt\Dom\Element; | ||
|
||
/** | ||
* Elements that can have the `checked` attribute (radio buttons and checkboxes) | ||
* are considered "Checkable", and their validity is dependent on other elements | ||
* of the same name within the form. | ||
*/ | ||
trait Checkable { | ||
/** @param string|array $value */ | ||
private function checkedValueIsAvailable(Element $element, string|array $value):bool { | ||
$availableValues = []; | ||
$name = $element->name; | ||
|
||
/** @var Element $otherInput */ | ||
foreach($element->form->getElementsByName($name) as $otherInput) { | ||
if($radioValue = $otherInput->value) { | ||
array_push($availableValues, $radioValue); | ||
} | ||
} | ||
|
||
$checkedValueList = []; | ||
if(is_array($value)) { | ||
$checkedValueList = $value; | ||
} | ||
else { | ||
$checkedValueList = [$value]; | ||
} | ||
|
||
foreach($checkedValueList as $checkedValue) { | ||
if(!in_array($checkedValue, $availableValues)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
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,37 @@ | ||
<?php | ||
namespace Gt\DomValidation\Rule; | ||
|
||
use Gt\Dom\Element; | ||
use Gt\Dom\ElementType; | ||
use Gt\DomValidation\Rule\Trait\Checkable; | ||
|
||
class TypeCheckbox extends Rule { | ||
use Checkable; | ||
|
||
public function isValid(Element $element, string|array $value, array $inputKvp):bool { | ||
if($element->elementType !== ElementType::HTMLInputElement) { | ||
return true; | ||
} | ||
if($element->type !== "checkbox") { | ||
return true; | ||
} | ||
|
||
if($value === "") { | ||
return true; | ||
} | ||
|
||
if(!$element->form) { | ||
return true; | ||
} | ||
|
||
if(!$this->checkedValueIsAvailable($element, $value)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function getHint(Element $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
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
Oops, something went wrong.