PHPackages                             voku/html-form-validator - 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. voku/html-form-validator

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

voku/html-form-validator
========================

HTML &lt;form&gt; validation without trouble, easy filtering &amp; validation.

3.0.1(4y ago)530[1 issues](https://github.com/voku/HtmlFormValidator/issues)[4 PRs](https://github.com/voku/HtmlFormValidator/pulls)MITPHPPHP ^7.3 || ^8.0CI passing

Since Dec 18Pushed 3w ago2 watchersCompare

[ Source](https://github.com/voku/HtmlFormValidator)[ Packagist](https://packagist.org/packages/voku/html-form-validator)[ RSS](/packages/voku-html-form-validator/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (5)Versions (18)Used By (0)

[![CI](https://github.com/voku/HtmlFormValidator/actions/workflows/ci.yml/badge.svg)](https://github.com/voku/HtmlFormValidator/actions/workflows/ci.yml)[![Coverage Status](https://camo.githubusercontent.com/01a5ddf406fc688d720218ce7ce1c172cd99aeb45262cf9660b61410b2bcbb89/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f766f6b752f48746d6c466f726d56616c696461746f722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/voku/HtmlFormValidator?branch=master)[![Codacy Badge](https://camo.githubusercontent.com/c08b8b44190bf595953f0b8ed63a319394e7fadc06dde3bc684b8dad157bd9d5/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3735323761356666643262393435643338633062353830626265336466643933)](https://www.codacy.com/app/voku/HtmlFormValidator?utm_source=github.com&utm_medium=referral&utm_content=voku/HtmlFormValidator&utm_campaign=Badge_Grade)[![Latest Stable Version](https://camo.githubusercontent.com/c3b0d05e62aa54302bca5cba2a099ba1f3fd3757035c30d9eb4eabdf5d2f7d68/68747470733a2f2f706f7365722e707567782e6f72672f766f6b752f68746d6c2d666f726d2d76616c696461746f722f762f737461626c65)](https://packagist.org/packages/voku/html-form-validator)[![Total Downloads](https://camo.githubusercontent.com/a3d27a36667f37df33002bd28bf230f2363d554c06e2ca9737c87369741a85fc/68747470733a2f2f706f7365722e707567782e6f72672f766f6b752f68746d6c2d666f726d2d76616c696461746f722f646f776e6c6f616473)](https://packagist.org/packages/voku/html-form-validator)[![License](https://camo.githubusercontent.com/200559d413e95697a0f28a6c8eb7f825b4264cfdc57bb7b0a7a23cbc7ceb0705/68747470733a2f2f706f7365722e707567782e6f72672f766f6b752f68746d6c2d666f726d2d76616c696461746f722f6c6963656e7365)](https://packagist.org/packages/voku/html-form-validator)[![Donate to this project using Paypal](https://camo.githubusercontent.com/0d6e4d8b50b5983a58205941b1a581b1305903393b7a39da574e3f60af3c7f5b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f70617970616c2d646f6e6174652d79656c6c6f772e737667)](https://www.paypal.me/moelleken)[![Donate to this project using Patreon](https://camo.githubusercontent.com/f9e075baad95563481d35174d43ef50757281abb6bc795d0f473fad452afa030/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f70617472656f6e2d646f6e6174652d79656c6c6f772e737667)](https://www.patreon.com/voku)

🔦 HTMLFormValidation
====================

[](#flashlight-htmlformvalidation)

### Description

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

[](#install-via-composer-require)

```
composer require voku/html-form-validator
```

### How does this work?

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

[](#simple-example)

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

[](#extended-example)

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

[](#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 -&gt; something like that -&gt; `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.

```
$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.

```
