{"id":20678913,"url":"https://github.com/phpgt/domvalidation","last_synced_at":"2025-03-10T19:26:59.026Z","repository":{"id":47542603,"uuid":"131306932","full_name":"phpgt/DomValidation","owner":"phpgt","description":"Server side form validation using web standards.","archived":false,"fork":false,"pushed_at":"2024-01-06T17:37:56.000Z","size":179,"stargazers_count":1,"open_issues_count":6,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-07T12:08:36.851Z","etag":null,"topics":["dom","form-validation","html-forms","html5","html5-validation","phpgt","webengine"],"latest_commit_sha":null,"homepage":"https://www.php.gt/domvalidation","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phpgt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["phpgt"]}},"created_at":"2018-04-27T14:27:56.000Z","updated_at":"2023-04-11T15:23:00.000Z","dependencies_parsed_at":"2024-01-05T17:27:35.016Z","dependency_job_id":"84f114cf-67f1-4173-bae0-dd67014d6f55","html_url":"https://github.com/phpgt/DomValidation","commit_stats":{"total_commits":64,"total_committers":1,"mean_commits":64.0,"dds":0.0,"last_synced_commit":"c5751b4517fa119028439fc87884738d2ded0cd7"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpgt%2FDomValidation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpgt%2FDomValidation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpgt%2FDomValidation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpgt%2FDomValidation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phpgt","download_url":"https://codeload.github.com/phpgt/DomValidation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242911396,"owners_count":20205487,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dom","form-validation","html-forms","html5","html5-validation","phpgt","webengine"],"created_at":"2024-11-16T21:23:06.679Z","updated_at":"2025-03-10T19:26:59.005Z","avatar_url":"https://github.com/phpgt.png","language":"PHP","readme":"Server side form validation using web standards.\n------------------------------------------------\n\nHTML 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.\n\nThis repository performs W3C form validation for projects that have a [server-side DOM][dom], such as within [WebEngine applications][webengine].\n\n***\n\n\u003ca href=\"https://github.com/PhpGt/DomValidation/actions\" target=\"_blank\"\u003e\n\t\u003cimg src=\"https://badge.status.php.gt/domvalidation-build.svg\" alt=\"Build status\" /\u003e\n\u003c/a\u003e\n\u003ca href=\"https://app.codacy.com/gh/PhpGt/DomValidation\" target=\"_blank\"\u003e\n\t\u003cimg src=\"https://badge.status.php.gt/domvalidation-quality.svg\" alt=\"Code quality\" /\u003e\n\u003c/a\u003e\n\u003ca href=\"https://app.codecov.io/gh/PhpGt/DomValidation\" target=\"_blank\"\u003e\n\t\u003cimg src=\"https://badge.status.php.gt/domvalidation-coverage.svg\" alt=\"Code coverage\" /\u003e\n\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/PhpGt/DomValidation\" target=\"_blank\"\u003e\n\t\u003cimg src=\"https://badge.status.php.gt/domvalidation-version.svg\" alt=\"Current version\" /\u003e\n\u003c/a\u003e\n\u003ca href=\"http://www.php.gt/domvalidation\" target=\"_blank\"\u003e\n\t\u003cimg src=\"https://badge.status.php.gt/domvalidation-docs.svg\" alt=\"PHP.Gt/DomValidation documentation\" /\u003e\n\u003c/a\u003e\n\n## Example usage\n\n```html\n\u003cform id=\"example-form\" method=\"post\"\u003e\n\t\u003clabel\u003e\n\t\t\u003cspan\u003eYour name\u003c/span\u003e\n\t\t\u003cinput name=\"name\" required /\u003e\n\t\u003c/label\u003e\n\t\u003clabel\u003e\n\t\t\u003cspan\u003eYour email\u003c/span\u003e\n\t\t\u003cinput name=\"email\" type=\"email\" required /\u003e\n\t\u003c/label\u003e\n\t\u003clabel\u003e\n\t\t\u003cspan\u003eYour account ID\u003c/span\u003e\n\t\t\u003cinput name=\"account\" pattern=\"\\S*\\d{,3}\" /\u003e\n\t\u003c/label\u003e\n\t\u003clabel\u003e\n\t\t\u003cspan\u003eYour nation\u003c/span\u003e\n\t\t\u003cselect name=\"nation\" required\u003e\n\t\t\t\u003coption\u003e\u003c/option\u003e\n\t\t\t\u003coption\u003eOceania\u003c/option\u003e\n\t\t\t\u003coption\u003eEurasia\u003c/option\u003e\n\t\t\t\u003coption\u003eEastasia\u003c/option\u003e\n\t\t\u003c/select\u003e\n\t\u003c/label\u003e\n\t\u003cbutton\u003eSubmit\u003c/button\u003e\n\u003c/form\u003e\n```\n\nThe 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.\n\nValidation rules present in the above HTML form:\n\n+ `name` input is required to be not empty.\n+ `email` input is required to be not empty, and must be a valid email address.\n+ `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).\n+ `nation` input must be one of the three enumerations present in the `\u003cselect\u003e` element.\n\n```php\nuse Gt\\Dom\\HTMLDocument;\nuse Gt\\DomValidation\\Validator;\nuse Gt\\DomValidation\\ValidationException;\n\n// Assume this function is triggered when POST data arrives.\nfunction handleSubmit($inputData) {\n\t$document = new HTMLDocument(file_get_contents(\"example-form.html\"));\n// First, obtain a reference to the form we wish to validate.\n\t$form = $document-\u003equerySelector(\"#example-form\");\n\t$validator = new Validator();\n\n\ttry {\n// Within a try/catch, pass the form and the user input into the Validator.\n\t\t$validator-\u003evalidate($form, $inputData);\n\t}\n\tcatch(ValidationException) {\n// If there are any validation errors, we can iterate over them to display\n// to the page, and return early as to not action the user input.\n\t\tforeach($validator-\u003egetLastErrorList() as $name =\u003e $message) {\n// Here we can add an attribute to the parent of the input, for displaying\n// the error message using CSS, for example.\n\t\t\t$errorElement = $form-\u003equerySelector(\"[name=$name]\");\n\t\t\t$errorElement-\u003eparentNode-\u003edataset-\u003evalidationError = $message;\n\t\t}\n        \n// Return early so user input isn't used when there are validation errors. \n\t\treturn;\n\t}\n\n// Finally, if the input contains no validation errors, continue as usual.\n\tsendInputToDatabase($inputData);\n}\n```\n\n## Supported validation mechanisms:\n\nIt's possible to add your own validation mechanism by extending the `FormValidator` class and overriding the necessary functions.\n\n+ `required` - field can not be left blank\n+ `pattern` - must match the provided regular expression\n+ `type` - must match the provided inbuilt data type\n+ `min` - for numerical inputs, the minimum allowed value\n+ `max` - for numerical inputs, the maximum allowed value\n+ `minlength` - the minimum number of characters allowed\n+ `maxlength` - the maximum number of characters allowed\n+ `step` - the granularity that is required\n\n### Supported types:\n\n+ `tel`\n+ `url`\n+ `email`\n+ `date`\n+ `month`\n+ `week`\n+ `time`\n+ `datetime-local`\n+ `number`\n+ `range`\n\n### Special element behaviour\n\nWhen using `\u003cselect\u003e` and `\u003cinput type=\"radio\" /\u003e` elements, their contained options are used as validation enumerations, meaning that values that are not part of the contained options will throw validation errors.\n\n[dom]: https://www.php.gt/dom\n[webengine]: https://www.php.gt/webengine\n","funding_links":["https://github.com/sponsors/phpgt"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphpgt%2Fdomvalidation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphpgt%2Fdomvalidation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphpgt%2Fdomvalidation/lists"}