| Recommend this page to a friend! | 
|  Download | 
| Info | Documentation |  Files |  Install with Composer |  Download | Reputation | Support forum | Blog | Links | 
| Ratings | Unique User Downloads | Download Rankings | ||||
| Not enough user ratings | Total: 230 | All time:  8,159 This week: 488  | ||||
| Version | License | PHP version | Categories | |||
| html-form-validator 1.0.0 | MIT/X Consortium ... | 7 | HTML, Validation, Parsers, PHP 7 | 
| Description | Author | |
| This package can validate submitted forms values with rules in HTML. | 
HtmlFormValidator is a very easy to use PHP library that will help you 
to validate your `<form>` data.
We will use Respect/Validation in the background, so you can use this independent from your framework of choice.
composer require voku/html-form-validator
use voku\HtmlFormValidator\Validator;
require_once 'composer/autoload.php';
$html = "
<form action="%s" id="register" method="post">
    <label for="email">Email:</label>
    <input
        type="email"
        id="email"
        name="user[email]"
        value=""
        data-validator="email"
        data-filter="trim"
        required="required"
    />
    
    <label for="username">Name:</label>
    <input
        type="text"
        id="username"
        name="user[name]"
        value=""
        data-validator="notEmpty|maxLength(100)"
        data-filter="strip_tags(<p>)|trim|escape"
        required="required"
    />
    
    <input type="submit"/>
</form>
";
$formValidator = new Validator($formHTML);
$formData = [
        'user' => [
            'email' => 'foo@isanemail',
            'name'  => 'bar',
        ],
    ];
// validate the form
$formValidatorResult = $formValidator->validate($formData);
// check the result
$formValidatorResult->isSuccess(); // false
// get the error messages
$formValidatorResult->getErrorMessages(); // ['user[email]' => ['"foo@isanemail" must be valid email']]    
You can use all validators from here.
e.g.: `data-validator="date"` (you need to lowercase the first letter from the class)
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 wan't 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"`
If you wan't to limit the submitted values to the values from the form e.g. for checkboxes or radios, then you can use "strict".
e.g.: `data-validator="strict"`
And if you need a more complex validation, then you can add simple-custom validations.
$formValidator->addCustomRule(
    'foobar',
    v::allOf(
        v::intVal(),
        v::positive()
    )
);
e.g.: `data-validator="foobar"`
And if you need really complex validation, then you can create your own classes.
<?php
namespace Respect\Validation\Rules;
class CustomRule extends AbstractRule
{
  /
   * @param string $value
   *
   * @return bool
   */
  public function validate($value)
  {
    return ($value === 'foobar');
  }
}
<?php
namespace Respect\Validation\Exceptions;
class CustomRuleException extends ValidationException
{
  public static $defaultTemplates = [
      self::MODE_DEFAULT  => [
          self::STANDARD => 'Invalid input... \'foobar\' is only allowed here... ', // eg: must be string
      ],
      self::MODE_NEGATIVE => [
          self::STANDARD => 'Invalid input... \'foobar\' is not allowed here... ', // eg: must not be string
      ],
  ];
}
$formValidator->addCustomRule('foobar', \Respect\Validation\Rules\CustomRule::class);
e.g.: `data-validator="foobar"`
You can also use some simple filters, that will be applied on the input-data.
e.g.: `data-filter="strip_tags(<p>)"`
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.
$formValidator->addCustomFilter(
    'append_lall',
    function ($input) {
      return $input . 'lall';
    }
);
e.g.: `data-filter="append_lall"`
1) Composer 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:
./vendor/bin/phpunit
|  Files (29) | 
| File | Role | Description | ||
|---|---|---|---|---|
|  src (1 directory) | ||||
|  tests (1 file, 1 directory) | ||||
|    .editorconfig | Data | Auxiliary data | ||
|    .scrutinizer.yml | Data | Auxiliary data | ||
|    .styleci.yml | Data | Auxiliary data | ||
|    .travis.yml | Data | Auxiliary data | ||
|    CHANGELOG.md | Data | Auxiliary data | ||
|    composer.json | Data | Auxiliary data | ||
|    CONTRIBUTING.md | Data | Auxiliary data | ||
|    ISSUE_TEMPLATE.md | Data | Auxiliary data | ||
|    LICENSE | Lic. | License text | ||
|    phpunit.xml | Data | Auxiliary data | ||
|    PULL_REQUEST_TEMPLATE.md | Data | Auxiliary data | ||
|    README.md | Doc. | Class source | ||
|  Files (29) | / | src | / | voku | / | HtmlFormValidator | 
| File | Role | Description | ||
|---|---|---|---|---|
|  Exceptions (5 files) | ||||
|  Rules (4 files) | ||||
|  Validator.php | Class | Class source | ||
|  ValidatorHelpers.php | Class | Class source | ||
|  ValidatorResult.php | Class | Class source | ||
|  ValidatorRulesManager.php | Class | Class source | ||
|  Files (29) | / | src | / | voku | / | HtmlFormValidator | / | Exceptions | 
| File | Role | Description | 
|---|---|---|
|  MaxLengthException.php | Class | Class source | 
|  MinLengthException.php | Class | Class source | 
|  NoValidationRule.php | Class | Class source | 
|  UnknownFilter.php | Class | Class source | 
|  UnknownValidationRule.php | Class | Class source | 
|  Files (29) | / | src | / | voku | / | HtmlFormValidator | / | Rules | 
| File | Role | Description | 
|---|---|---|
|  Auto.php | Class | Class source | 
|  MaxLength.php | Class | Class source | 
|  MinLength.php | Class | Class source | 
|  Strict.php | Class | Class source | 
|  Files (29) | / | tests | / | unit | 
| File | Role | Description | 
|---|---|---|
|  CustomRule.php | Class | Class source | 
|  CustomRuleException.php | Class | Class source | 
|  ValidatorTest.php | Class | Class source | 
| The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. | 
|  Install with Composer | 
|  | html-form-validator-2017-12-17.zip 21KB | 
|  | html-form-validator-2017-12-17.tar.gz 14KB | 
|  | Install with Composer | 
| Needed packages | ||
| Class | Download | Why it is needed | Dependency | 
|---|---|---|---|
| Portable UTF-8 |  .zip  .tar.gz | UTF-8 support | Required | 
| Simple HTML DOM |  .zip  .tar.gz | HTML-Dom interactions | Required | 
| Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
| 100% | 
 | 
 | 
| Applications that use this package | 
 If you know an application of this package, send a message to the author to add a link here.
 If you know an application of this package, send a message to the author to add a link here.