Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to use PHP.Gt/DOM's facade #20

Merged
merged 10 commits into from
Aug 24, 2021
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,17 @@ Validation rules present in the above HTML form:

+ `name` input is required to be not empty.
+ `email` input is required to be not empty, and must be a valid email address.
+ `account` input is not required, but when a value is submitted, it must match the provided regular expression (any number of non-whitespace characters followed by up to 3 numbers).
+ `nation` input must be one of the three enumerations present in the `<select>` element.

```php
use \Gt\DomValidation\Validator;
use \Gt\DomValidation\ValidationException;
use Gt\Dom\HTMLDocument;
use Gt\DomValidation\Validator;
use Gt\DomValidation\ValidationException;

// Assume this function is triggered when POST data arrives.
function handleSubmit($inputData) {
$document = new HTMLDocument(file_get_contents("example-form.html"));
// First, obtain a reference to the form we wish to validate.
$form = $document->querySelector("#example-form");
$validator = new Validator();
Expand All @@ -74,7 +77,7 @@ function handleSubmit($inputData) {
// Within a try/catch, pass the form and the user input into the Validator.
$validator->validate($form, $inputData);
}
catch(ValidationException $exception) {
catch(ValidationException) {
// If there are any validation errors, we can iterate over them to display
// to the page, and return early as to not action the user input.
foreach($validator->getLastErrorList() as $name => $message) {
Expand Down Expand Up @@ -124,4 +127,4 @@ It's possible to add your own validation mechanism by extending the `FormValidat
When using `<select>` and `<input type="radio" />` elements, their contained options are used as validation enumerations, meaning that values that are not part of the contained options will throw validation errors.

[dom]: https://www.php.gt/dom
[webengine]: https://www.php.gt/webengine
[webengine]: https://www.php.gt/webengine
12 changes: 9 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@

"require": {
"php": ">=8.0",
"ext-dom": "*",
"phpgt/cssxpath": "1.*",
"phpgt/dom": "dev-facade"
"phpgt/dom": "dev-master"
},

"require-dev": {
Expand All @@ -29,5 +28,12 @@
"psr-4": {
"Gt\\DomValidation\\Test\\": "./test/phpunit"
}
}
},

"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/PhpGt"
}
]
}
Loading