Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* build: hard dependencies * test: ensure asArray is called * maintenance: phpstan * test: code sniffer tests * wip: mess detector - working on reducing complexity * test: improve tests * test: improve coverage
- Loading branch information
Showing
16 changed files
with
233 additions
and
120 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,82 @@ | ||
<?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/01-shop.php in your web browser. | ||
*/ | ||
|
||
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> | ||
<form method="post"> | ||
<label> | ||
<span>Your name</span> | ||
<input name="name" required /> | ||
</label> | ||
<label> | ||
<span>Credit Card number</span> | ||
<input name="credit-card" pattern="\d{16}" required /> | ||
</label> | ||
<label> | ||
<span>Expiry month</span> | ||
<input name="expiry-month" type="number" min="1" max="12" required /> | ||
</label> | ||
<label> | ||
<span>Expiry year</span> | ||
<input name="expiry-year" type="number" min="18" max="26" required /> | ||
</label> | ||
<label> | ||
<span>amount</span> | ||
<input name="amount" type="number" value="100.50" readonly required /> | ||
</label> | ||
<button name="do" value="buy">Buy the thing!</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 "Payment succeeded!"; | ||
exit; | ||
} | ||
|
||
$document = new HTMLDocument($html); | ||
|
||
if(isset($_POST["do"]) && $_POST["do"] === "buy") { | ||
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
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.