https://github.com/phpgt/domvalidation
Server side form validation using web standards.
https://github.com/phpgt/domvalidation
dom form-validation html-forms html5 html5-validation phpgt webengine
Last synced: about 1 month ago
JSON representation
Server side form validation using web standards.
- Host: GitHub
- URL: https://github.com/phpgt/domvalidation
- Owner: phpgt
- License: mit
- Created: 2018-04-27T14:27:56.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-01-06T17:37:56.000Z (over 1 year ago)
- Last Synced: 2025-03-07T12:08:36.851Z (about 2 months ago)
- Topics: dom, form-validation, html-forms, html5, html5-validation, phpgt, webengine
- Language: PHP
- Homepage: https://www.php.gt/domvalidation
- Size: 175 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
Server side form validation using web standards.
------------------------------------------------HTML forms can be annotated in such a way that the individual input elements can describe their own validation rules. The simplest annotation is the `required` attribute, which can be specified on input elements to indicate that the form is not to be submitted until a value is given.
This repository performs W3C form validation for projects that have a [server-side DOM][dom], such as within [WebEngine applications][webengine].
***
## Example usage
```html
Your name
Your email
Your account ID
Your nation
Oceania
Eurasia
Eastasia
Submit```
The above HTML will be validated on the client as usual, but using the PHP below will also provide server-side validation without any additional validation logic to be written.
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 `` element.```php
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();try {
// Within a try/catch, pass the form and the user input into the Validator.
$validator->validate($form, $inputData);
}
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) {
// Here we can add an attribute to the parent of the input, for displaying
// the error message using CSS, for example.
$errorElement = $form->querySelector("[name=$name]");
$errorElement->parentNode->dataset->validationError = $message;
}
// Return early so user input isn't used when there are validation errors.
return;
}// Finally, if the input contains no validation errors, continue as usual.
sendInputToDatabase($inputData);
}
```## Supported validation mechanisms:
It's possible to add your own validation mechanism by extending the `FormValidator` class and overriding the necessary functions.
+ `required` - field can not be left blank
+ `pattern` - must match the provided regular expression
+ `type` - must match the provided inbuilt data type
+ `min` - for numerical inputs, the minimum allowed value
+ `max` - for numerical inputs, the maximum allowed value
+ `minlength` - the minimum number of characters allowed
+ `maxlength` - the maximum number of characters allowed
+ `step` - the granularity that is required### Supported types:
+ `tel`
+ `url`
+ `email`
+ `date`
+ `month`
+ `week`
+ `time`
+ `datetime-local`
+ `number`
+ `range`### Special element behaviour
When using `` and `` 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