{"id":15729144,"url":"https://github.com/voku/htmlformvalidator","last_synced_at":"2025-03-13T03:30:49.111Z","repository":{"id":40696443,"uuid":"114509957","full_name":"voku/HtmlFormValidator","owner":"voku","description":":flashlight: HtmlFormValidator | HTML Form Validation via DOM Parsing","archived":false,"fork":false,"pushed_at":"2024-02-03T01:07:46.000Z","size":91,"stargazers_count":4,"open_issues_count":3,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-29T22:48:33.161Z","etag":null,"topics":["filter","form","form-validation","hacktoberfest","html","html-form","php","validation","validator"],"latest_commit_sha":null,"homepage":"","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/voku.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"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}},"created_at":"2017-12-17T04:43:52.000Z","updated_at":"2023-09-27T00:47:02.000Z","dependencies_parsed_at":"2024-02-25T04:00:24.872Z","dependency_job_id":"584bec41-d51f-427f-99fb-f2a55f9b849d","html_url":"https://github.com/voku/HtmlFormValidator","commit_stats":{"total_commits":52,"total_committers":5,"mean_commits":10.4,"dds":"0.28846153846153844","last_synced_commit":"3947a8f29a9680ce3bb615742a5e79caa901202d"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/voku%2FHtmlFormValidator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/voku%2FHtmlFormValidator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/voku%2FHtmlFormValidator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/voku%2FHtmlFormValidator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/voku","download_url":"https://codeload.github.com/voku/HtmlFormValidator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243335143,"owners_count":20274896,"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":["filter","form","form-validation","hacktoberfest","html","html-form","php","validation","validator"],"created_at":"2024-10-03T23:20:26.164Z","updated_at":"2025-03-13T03:30:48.813Z","avatar_url":"https://github.com/voku.png","language":"PHP","funding_links":["https://www.paypal.me/moelleken","https://www.patreon.com/voku","https://paypal.me/moelleken","https://www.patreon.com/voku/posts"],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/voku/HtmlFormValidator.svg?branch=master)](https://travis-ci.org/voku/HtmlFormValidator)\n[![Coverage Status](https://coveralls.io/repos/github/voku/HtmlFormValidator/badge.svg?branch=master)](https://coveralls.io/github/voku/HtmlFormValidator?branch=master)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/7527a5ffd2b945d38c0b580bbe3dfd93)](https://www.codacy.com/app/voku/HtmlFormValidator?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=voku/HtmlFormValidator\u0026amp;utm_campaign=Badge_Grade)\n[![Latest Stable Version](https://poser.pugx.org/voku/html-form-validator/v/stable)](https://packagist.org/packages/voku/html-form-validator) \n[![Total Downloads](https://poser.pugx.org/voku/html-form-validator/downloads)](https://packagist.org/packages/voku/html-form-validator) \n[![License](https://poser.pugx.org/voku/html-form-validator/license)](https://packagist.org/packages/voku/html-form-validator)\n[![Donate to this project using Paypal](https://img.shields.io/badge/paypal-donate-yellow.svg)](https://www.paypal.me/moelleken)\n[![Donate to this project using Patreon](https://img.shields.io/badge/patreon-donate-yellow.svg)](https://www.patreon.com/voku)\n\n# :flashlight: HTMLFormValidation \n\n### Description\n\nHtmlFormValidator is a very easy to use PHP library that will help you \nto validate your ```\u003cform\u003e``` data and you can use this independent from your framework of choice.\n\n\n### Install via \"composer require\"\n\n```shell\ncomposer require voku/html-form-validator\n```\n\n\n### How does this work?\n\n1. First you need to generate a html form, that's completely your part. You can write it manually or you can generate with a framework or a library, it doesn't matter.\n\n2. Then we use DOM Parsing via [voku/simple_html_dom](https://github.com/voku/simple_html_dom), to detect the current validator and filter rules directly from the html.\n\n3. And finally, we use [Respect/Validation](https://github.com/Respect/Validation) to validate the form.\n\n\n### Simple Example\n\n```php\nuse voku\\HtmlFormValidator\\Validator;\n\nrequire_once 'composer/autoload.php';\n\n$html = '\n\u003cform id=\"music\" method=\"post\"\u003e\n  \u003clabel\u003eArtist:\n    \u003cselect name=\"top5\" required=\"required\"\u003e\n      \u003coption\u003eHeino\u003c/option\u003e\n      \u003coption\u003eMichael Jackson\u003c/option\u003e\n      \u003coption\u003eTom Waits\u003c/option\u003e\n      \u003coption\u003eNina Hagen\u003c/option\u003e\n      \u003coption\u003eMarianne Rosenberg\u003c/option\u003e\n    \u003c/select\u003e\n  \u003c/label\u003e\n\u003c/form\u003e\n';\n\n$rules = $formValidator-\u003egetAllRules();\nstatic::assertSame(\n    [\n        'music' =\u003e [\n            'top5' =\u003e 'in(' . \\serialize(['Heino','Michael Jackson','Tom Waits','Nina Hagen','Marianne Rosenberg',]) . ')',\n        ],\n    ],\n    $rules\n);\n\n// --- valid\n\n// fake some data\n$_POST = [\n    'top5' =\u003e 'Heino',\n];\n$formValidatorResult = $formValidator-\u003evalidate($_POST);\nstatic::assertSame([], $formValidatorResult-\u003egetErrorMessages());\n\n// --- invalid\n\n// fake some data\n$_POST = [\n    'top5' =\u003e 'fooooo',\n];\n$formValidatorResult = $formValidator-\u003evalidate($_POST);\nstatic::assertSame(\n    [\n        'top5' =\u003e [\n            '\"fooooo\" must be in { \"Heino\", \"Michael Jackson\", \"Tom Waits\", \"Nina Hagen\", \"Marianne Rosenberg\" }',\n        ],\n    ],\n    $formValidatorResult-\u003egetErrorMessages()\n);\n```\n\n### Extended Example\n\n```php\nuse voku\\HtmlFormValidator\\Validator;\n\nrequire_once 'composer/autoload.php';\n\n$html = '\n\u003cform id=\"register\" method=\"post\"\u003e\n    \u003clabel for=\"email\"\u003eEmail:\u003c/label\u003e\n    \u003cinput\n        type=\"email\"\n        id=\"email\"\n        name=\"user[email]\"\n        value=\"\"\n        data-validator=\"email\"\n        data-filter=\"trim\"\n        data-error-class=\"error-foo-bar\"\n        data-error-message--email=\"Your email [%s] address is not correct.\"\n        data-error-template-selector=\"span#email-error-message-template\"\n        required=\"required\"\n    \u003e\n    \u003cspan style=\"color: red;\" id=\"email-error-message-template\"\u003e\u003c/span\u003e\n    \n    \u003clabel for=\"username\"\u003eName:\u003c/label\u003e\n    \u003cinput\n        type=\"text\"\n        id=\"username\"\n        name=\"user[name]\"\n        value=\"\"\n        data-validator=\"notEmpty|maxLength(100)\"\n        data-filter=\"strip_tags(\u003cp\u003e)|trim|escape\"\n        data-error-class=\"error-foo-bar\"\n        data-error-template-selector=\"span#username-error-message-template\"\n        required=\"required\"\n    \u003e\n    \u003cspan style=\"color: red;\" id=\"username-error-message-template\"\u003e\u003c/span\u003e\n    \n    \u003clabel for=\"date\"\u003eDate:\u003c/label\u003e\n    \u003cinput \n        type=\"text\"\n        id=\"date\"\n        name=\"user[date]\"\n        value=\"\"\n        data-validator=\"dateGerman|notEmpty\"\n        data-filter=\"trim\"\n        data-error-class=\"error-foo-bar\"\n        data-error-message--dateGerman=\"Date is not correct.\"\n        data-error-message--notEmpty=\"Date is empty.\"\n        data-error-template-selector=\"span#date-error-message-template\"\n        required=\"required\"\n    \u003e\n    \u003cspan style=\"color: red;\" id=\"date-error-message-template\"\u003e\u003c/span\u003e\n    \n    \u003cbutton type=\"submit\"\u003esubmit\u003c/button\u003e\n\u003c/form\u003e\n';\n\n$formValidator = new Validator($html);\n\n// fake some data\n$_POST = [\n    'user' =\u003e [\n        'email' =\u003e 'foo@isanemail',\n        'name'  =\u003e 'bar',\n    ],\n];\n\n// validate the form\n$formValidatorResult = $formValidator-\u003evalidate($_POST);\n\n// check the result\nstatic::assertFalse($formValidatorResult-\u003eisSuccess());\n\n// get the error messages\nstatic::assertSame(\n    [\n        'user[email]' =\u003e ['Your email [foo@isanemail] address is not correct.'],\n        'user[date]'  =\u003e [\n            'Date is not correct.',\n            'Date is empty.',\n        ],\n    ],\n    $formValidatorResult-\u003egetErrorMessages()\n);\n\n// get the new html\nstatic::assertSame(\n    '\n    \u003cform id=\"register\" method=\"post\"\u003e\n        \u003clabel for=\"email\"\u003eEmail:\u003c/label\u003e\n        \u003cinput \n                type=\"email\" \n                id=\"email\" \n                name=\"user[email]\" \n                value=\"\" \n                data-validator=\"email\" \n                data-filter=\"trim\" \n                data-error-class=\"error-foo-bar\" \n                data-error-message--email=\"Your email [%s] address is not correct.\" \n                data-error-template-selector=\"span#email-error-message-template\" \n                required=\"required\" \n                aria-invalid=\"true\"\n        \u003e            \n        \u003cspan style=\"color: red;\" id=\"email-error-message-template\"\u003eYour email [foo@isanemail] address is not correct.\u003c/span\u003e\n                                \n        \u003clabel for=\"username\"\u003eName:\u003c/label\u003e\n        \u003cinput \n                type=\"text\" \n                id=\"username\" \n                name=\"user[name]\" \n                value=\"bar\" \n                data-validator=\"notEmpty|maxLength(100)\" \n                data-filter=\"strip_tags(\u003cp\u003e)|trim|escape\" \n                data-error-class=\"error-foo-bar\" \n                data-error-template-selector=\"span#username-error-message-template\" \n                required=\"required\" \n                aria-invalid=\"false\"\n        \u003e            \n        \u003cspan style=\"color: red;\" id=\"username-error-message-template\"\u003e\u003c/span\u003e\n                                \n        \u003clabel for=\"date\"\u003eDate:\u003c/label\u003e\n        \u003cinput \n                type=\"text\" \n                id=\"date\" \n                name=\"user[date]\" \n                value=\"\" \n                data-validator=\"dateGerman|notEmpty\" \n                data-filter=\"trim\" \n                data-error-class=\"error-foo-bar\" \n                data-error-message--dategerman=\"Date is not correct.\" \n                data-error-message--notempty=\"Date is empty.\" \n                data-error-template-selector=\"span#date-error-message-template\"\n                required=\"required\" \n                aria-invalid=\"true\"\n        \u003e            \n        \u003cspan style=\"color: red;\" id=\"date-error-message-template\"\u003eDate is not correct. Date is empty.\u003c/span\u003e\n                                \n        \u003cbutton type=\"submit\"\u003esubmit\u003c/button\u003e\n    \u003c/form\u003e\n    ',\n    $formValidatorResult-\u003egetHtml()\n);\n```\n\n### Validator\n\nYou can use all validators from [here](https://github.com/Respect/Validation/blob/1.1/docs/VALIDATORS.md).\n\ne.g.: ```data-validator=\"date\"``` || ```data-validator=\"' . \\Respect\\Validation\\Rules\\Date::class . '\"```  (you need to lowercase the first letter from the class or you can use the class name itself)\n\nYou can combine validators simply via \"|\" ...\n\ne.g.: ```data-validator=\"notEmpty|maxLength(100)\"```\n\nPS: you can add arguments comma separated or you can use serialize -\u003e something like that -\u003e ```in(' . serialize($selectableValues) . ')```\n\nIf you want to use the HTML5 validation e.g. for min or max values, or for e.g. email then you can use \"auto\".\n\ne.g.: ```data-validator=\"auto\"```\n\nBy default we limit the submitted values to the values from the form e.g. for checkboxes, radios or select boxes. If you need to disable this,\nyou can use \"non-strict\". (not recommended)\n\ne.g.: ```data-validator=\"non-strict\"```\n\nBy default we use the error messages from the validation exception class, but you can use your own error messages via:\n\"data-error-message--RULE_NAME_HERE\" in the html.\n\ne.g.: ```data-error-message--email=\"Email [%s] is not correct\"```\n\nBy default we don't add error messages into html output, but you can add the error messages with a css selector:\n\ne.g.: ```data-error-template-selector=\"span#email-error-message-template\"```\n\nBy default we also don't add error classes, but you can add a new error class via:\n\ne.g. ```data-error-class=\"error-foo-bar\"```\n\nAnd if you need a more complex validation, then you can add simple-custom validations.\n\n```php\n$formValidator-\u003eaddCustomRule(\n    'foobar',\n    \\Respect\\Validation\\Validator::allOf(\n        \\Respect\\Validation\\Validator::intVal(),\n        \\Respect\\Validation\\Validator::positive()\n    )\n);\n```\n\ne.g.: ```data-validator=\"foobar\"```\n\nAnd if you need really complex validation, then you can create your own classes.\n\n```php\n\u003c?php\n\nnamespace Respect\\Validation\\Rules;\n\nclass CustomRule extends AbstractRule\n{\n  /**\n   * @param string $value\n   *\n   * @return bool\n   */\n  public function validate($value)\n  {\n    return ($value === 'foobar');\n  }\n\n}\n```\n\n```php\n\u003c?php\n\nnamespace Respect\\Validation\\Exceptions;\n\nclass CustomRuleException extends ValidationException\n{\n  public static $defaultTemplates = [\n      self::MODE_DEFAULT  =\u003e [\n          self::STANDARD =\u003e 'Invalid input... \\'foobar\\' is only allowed here... ',\n      ],\n      self::MODE_NEGATIVE =\u003e [\n          self::STANDARD =\u003e 'Invalid input... \\'foobar\\' is not allowed here... ',\n      ],\n  ];\n}\n```\n\n```php\n$formValidator-\u003eaddCustomRule('foobar', \\Respect\\Validation\\Rules\\CustomRule::class);\n```\n\ne.g.: ```data-validator=\"foobar\"```\n\n\n### Filter\n\nYou can also use some simple filters, that will be applied on the input-data.\n\n- trim\n- escape (htmlentities with ENT_QUOTES | ENT_HTML5)\n- ... and all methods from [here](https://github.com/voku/portable-utf8/blob/master/README.md)\n\ne.g.: ```data-filter=\"strip_tags(\u003cp\u003e)\"```\n\nPS: the first argument will be the submitted value from the user\n\nAnd also here you can combine some filters simply via \"|\" ...\n\ne.g.: ```data-filter=\"strip_tags|trim|escape\"```\n\n... and you can also add custom filters by your own.\n\n```php\n$formValidator-\u003eaddCustomFilter(\n    'append_lall',\n    function ($input) {\n      return $input . 'lall';\n    }\n);\n```\n\ne.g.: ```data-filter=\"append_lall\"```\n\n\n### Unit Test\n\n1) [Composer](https://getcomposer.org) is a prerequisite for running the tests.\n\n```\ncomposer install voku/HtmlFormValidator\n```\n\n2) The tests can be executed by running this command from the root directory:\n\n```bash\n./vendor/bin/phpunit\n```\n\n### Support\n\nFor support and donations please visit [Github](https://github.com/voku/HtmlFormValidator/) | [Issues](https://github.com/voku/HtmlFormValidator/issues) | [PayPal](https://paypal.me/moelleken) | [Patreon](https://www.patreon.com/voku).\n\nFor status updates and release announcements please visit [Releases](https://github.com/voku/HtmlFormValidator/releases) | [Twitter](https://twitter.com/suckup_de) | [Patreon](https://www.patreon.com/voku/posts).\n\nFor professional support please contact [me](https://about.me/voku).\n\n### Thanks\n\n- Thanks to [GitHub](https://github.com) (Microsoft) for hosting the code and a good infrastructure including Issues-Managment, etc.\n- Thanks to [IntelliJ](https://www.jetbrains.com) as they make the best IDEs for PHP and they gave me an open source license for PhpStorm!\n- Thanks to [Travis CI](https://travis-ci.com/) for being the most awesome, easiest continous integration tool out there!\n- Thanks to [StyleCI](https://styleci.io/) for the simple but powerfull code style check.\n- Thanks to [PHPStan](https://github.com/phpstan/phpstan) \u0026\u0026 [Psalm](https://github.com/vimeo/psalm) for relly great Static analysis tools and for discover bugs in the code!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvoku%2Fhtmlformvalidator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvoku%2Fhtmlformvalidator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvoku%2Fhtmlformvalidator/lists"}