PHPackages                             phpgt/domvalidation - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. phpgt/domvalidation

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

phpgt/domvalidation
===================

Server side form validation using web standards.

v1.0.2(2mo ago)1357↓50%[1 PRs](https://github.com/PhpGt/DomValidation/pulls)1MITPHPPHP &gt;=8.2CI failing

Since Dec 29Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/PhpGt/DomValidation)[ Packagist](https://packagist.org/packages/phpgt/domvalidation)[ GitHub Sponsors](https://github.com/sponsors/PhpGt)[ RSS](/packages/phpgt-domvalidation/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (12)Versions (17)Used By (1)

Server side form validation using web standards.
------------------------------------------------

[](#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](https://www.php.gt/dom), such as within [WebEngine applications](https://www.php.gt/webengine).

---

[ ![Build status](https://camo.githubusercontent.com/dce1005b9fe03cdeba4ff82aa7bf7b14301a357880adbe777c02e0b6bb325357/68747470733a2f2f62616467652e7374617475732e7068702e67742f646f6d76616c69646174696f6e2d6275696c642e737667)](https://github.com/PhpGt/DomValidation/actions)[ ![Code quality](https://camo.githubusercontent.com/f77dc319b5f1ff039aff2c86713d7dca65213a0f1ee809d5a57b7a9cc6d8f953/68747470733a2f2f62616467652e7374617475732e7068702e67742f646f6d76616c69646174696f6e2d7175616c6974792e737667)](https://app.codacy.com/gh/PhpGt/DomValidation)[ ![Code coverage](https://camo.githubusercontent.com/edb24e0be7458b02f57ca19fe0e0aa965e95b758df6c378882eeaa0f65d9685d/68747470733a2f2f62616467652e7374617475732e7068702e67742f646f6d76616c69646174696f6e2d636f7665726167652e737667)](https://app.codecov.io/gh/PhpGt/DomValidation)[ ![Current version](https://camo.githubusercontent.com/f98ae43220de2efccaad71b0bb78e1bde632486dc53c3428674a94c562bfafba/68747470733a2f2f62616467652e7374617475732e7068702e67742f646f6d76616c69646174696f6e2d76657273696f6e2e737667)](https://packagist.org/packages/PhpGt/DomValidation)[ ![PHP.Gt/DomValidation documentation](https://camo.githubusercontent.com/e50c73a02b60207a131a796fe546deceb78828c04cd928d727fce7212751ca1c/68747470733a2f2f62616467652e7374617475732e7068702e67742f646f6d76616c69646174696f6e2d646f63732e737667)](http://www.php.gt/domvalidation)Example usage
-------------

[](#example-usage)

```

		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.

```
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:
--------------------------------

[](#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:

[](#supported-types)

- `tel`
- `url`
- `email`
- `date`
- `month`
- `week`
- `time`
- `datetime-local`
- `number`
- `range`

### Special element behaviour

[](#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.

Proudly sponsored by
====================

[](#proudly-sponsored-by)

[JetBrains Open Source sponsorship program](https://www.jetbrains.com/community/opensource/)

[![JetBrains logo.](https://camo.githubusercontent.com/b5639e7738c6dfae9fe3f3e20175570b7376ce2577a772e09c25c2d4f14bf86e/68747470733a2f2f7265736f75726365732e6a6574627261696e732e636f6d2f73746f726167652f70726f64756374732f636f6d70616e792f6272616e642f6c6f676f732f6a6574627261696e732e737667)](https://www.jetbrains.com/community/opensource/)

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance88

Actively maintained with recent releases

Popularity19

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity78

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~205 days

Recently: every ~467 days

Total

12

Last Release

68d ago

Major Versions

v0.3.2 → v1.0.02023-02-12

PHP version history (5 changes)v0.0.1PHP &gt;=7.2

v0.3.1PHP &gt;=7.4

v0.3.2PHP &gt;=8.0

v1.0.0PHP &gt;=8.1

v1.0.2PHP &gt;=8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/9e42344b91ce4b91ab57875969f67a0a6a48de570a08bc65d673b06b72fd3a3f?d=identicon)[g105b](/maintainers/g105b)

---

Top Contributors

[![g105b](https://avatars.githubusercontent.com/u/358014?v=4)](https://github.com/g105b "g105b (8 commits)")

---

Tags

domform-validationhtml-formshtml5html5-validationphpgtwebengine

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/phpgt-domvalidation/health.svg)

```
[![Health](https://phpackages.com/badges/phpgt-domvalidation/health.svg)](https://phpackages.com/packages/phpgt-domvalidation)
```

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
