https://github.com/voku/htmlformvalidator
:flashlight: HtmlFormValidator | HTML Form Validation via DOM Parsing
https://github.com/voku/htmlformvalidator
filter form form-validation hacktoberfest html html-form php validation validator
Last synced: 7 months ago
JSON representation
:flashlight: HtmlFormValidator | HTML Form Validation via DOM Parsing
- Host: GitHub
- URL: https://github.com/voku/htmlformvalidator
- Owner: voku
- License: mit
- Created: 2017-12-17T04:43:52.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2024-02-03T01:07:46.000Z (over 1 year ago)
- Last Synced: 2024-10-29T22:48:33.161Z (11 months ago)
- Topics: filter, form, form-validation, hacktoberfest, html, html-form, php, validation, validator
- Language: PHP
- Homepage:
- Size: 88.9 KB
- Stars: 4
- Watchers: 4
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/voku/HtmlFormValidator)
[](https://coveralls.io/github/voku/HtmlFormValidator?branch=master)
[](https://www.codacy.com/app/voku/HtmlFormValidator?utm_source=github.com&utm_medium=referral&utm_content=voku/HtmlFormValidator&utm_campaign=Badge_Grade)
[](https://packagist.org/packages/voku/html-form-validator)
[](https://packagist.org/packages/voku/html-form-validator)
[](https://packagist.org/packages/voku/html-form-validator)
[](https://www.paypal.me/moelleken)
[](https://www.patreon.com/voku)# :flashlight: HTMLFormValidation
### Description
HtmlFormValidator is a very easy to use PHP library that will help you
to validate your `````` data and you can use this independent from your framework of choice.### Install via "composer require"
```shell
composer require voku/html-form-validator
```### How does this work?
1. 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.
2. 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.
3. And finally, we use [Respect/Validation](https://github.com/Respect/Validation) to validate the form.
### Simple Example
```php
use voku\HtmlFormValidator\Validator;require_once 'composer/autoload.php';
$html = '
Artist:
Heino
Michael Jackson
Tom Waits
Nina Hagen
Marianne Rosenberg
';
$rules = $formValidator->getAllRules();
static::assertSame(
[
'music' => [
'top5' => 'in(' . \serialize(['Heino','Michael Jackson','Tom Waits','Nina Hagen','Marianne Rosenberg',]) . ')',
],
],
$rules
);// --- valid
// fake some data
$_POST = [
'top5' => 'Heino',
];
$formValidatorResult = $formValidator->validate($_POST);
static::assertSame([], $formValidatorResult->getErrorMessages());// --- invalid
// fake some data
$_POST = [
'top5' => 'fooooo',
];
$formValidatorResult = $formValidator->validate($_POST);
static::assertSame(
[
'top5' => [
'"fooooo" must be in { "Heino", "Michael Jackson", "Tom Waits", "Nina Hagen", "Marianne Rosenberg" }',
],
],
$formValidatorResult->getErrorMessages()
);
```### Extended Example
```php
use voku\HtmlFormValidator\Validator;require_once 'composer/autoload.php';
$html = '
Email:
Name:
Date:
submit';
$formValidator = new Validator($html);
// fake some data
$_POST = [
'user' => [
'email' => 'foo@isanemail',
'name' => 'bar',
],
];// validate the form
$formValidatorResult = $formValidator->validate($_POST);// check the result
static::assertFalse($formValidatorResult->isSuccess());// get the error messages
static::assertSame(
[
'user[email]' => ['Your email [foo@isanemail] address is not correct.'],
'user[date]' => [
'Date is not correct.',
'Date is empty.',
],
],
$formValidatorResult->getErrorMessages()
);// get the new html
static::assertSame(
'
Email:
Your email [foo@isanemail] address is not correct.
Name:
Date:
Date is not correct. Date is empty.
submit
',
$formValidatorResult->getHtml()
);
```### Validator
You can use all validators from [here](https://github.com/Respect/Validation/blob/1.1/docs/VALIDATORS.md).
e.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)
You can combine validators simply via "|" ...
e.g.: ```data-validator="notEmpty|maxLength(100)"```
PS: you can add arguments comma separated or you can use serialize -> something like that -> ```in(' . serialize($selectableValues) . ')```
If you want to use the HTML5 validation e.g. for min or max values, or for e.g. email then you can use "auto".
e.g.: ```data-validator="auto"```
By 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,
you can use "non-strict". (not recommended)e.g.: ```data-validator="non-strict"```
By default we use the error messages from the validation exception class, but you can use your own error messages via:
"data-error-message--RULE_NAME_HERE" in the html.e.g.: ```data-error-message--email="Email [%s] is not correct"```
By default we don't add error messages into html output, but you can add the error messages with a css selector:
e.g.: ```data-error-template-selector="span#email-error-message-template"```
By default we also don't add error classes, but you can add a new error class via:
e.g. ```data-error-class="error-foo-bar"```
And if you need a more complex validation, then you can add simple-custom validations.
```php
$formValidator->addCustomRule(
'foobar',
\Respect\Validation\Validator::allOf(
\Respect\Validation\Validator::intVal(),
\Respect\Validation\Validator::positive()
)
);
```e.g.: ```data-validator="foobar"```
And if you need really complex validation, then you can create your own classes.
```php
[
self::STANDARD => 'Invalid input... \'foobar\' is only allowed here... ',
],
self::MODE_NEGATIVE => [
self::STANDARD => 'Invalid input... \'foobar\' is not allowed here... ',
],
];
}
``````php
$formValidator->addCustomRule('foobar', \Respect\Validation\Rules\CustomRule::class);
```e.g.: ```data-validator="foobar"```
### Filter
You can also use some simple filters, that will be applied on the input-data.
- trim
- escape (htmlentities with ENT_QUOTES | ENT_HTML5)
- ... and all methods from [here](https://github.com/voku/portable-utf8/blob/master/README.md)e.g.: ```data-filter="strip_tags(
)"```
PS: the first argument will be the submitted value from the user
And also here you can combine some filters simply via "|" ...
e.g.: ```data-filter="strip_tags|trim|escape"```
... and you can also add custom filters by your own.
```php
$formValidator->addCustomFilter(
'append_lall',
function ($input) {
return $input . 'lall';
}
);
```e.g.: ```data-filter="append_lall"```
### Unit Test
1) [Composer](https://getcomposer.org) is a prerequisite for running the tests.
```
composer install voku/HtmlFormValidator
```2) The tests can be executed by running this command from the root directory:
```bash
./vendor/bin/phpunit
```### Support
For 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).
For 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).
For professional support please contact [me](https://about.me/voku).
### Thanks
- Thanks to [GitHub](https://github.com) (Microsoft) for hosting the code and a good infrastructure including Issues-Managment, etc.
- 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!
- Thanks to [Travis CI](https://travis-ci.com/) for being the most awesome, easiest continous integration tool out there!
- Thanks to [StyleCI](https://styleci.io/) for the simple but powerfull code style check.
- Thanks to [PHPStan](https://github.com/phpstan/phpstan) && [Psalm](https://github.com/vimeo/psalm) for relly great Static analysis tools and for discover bugs in the code!