PHPackages                             fishfin/php-valigator - 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. fishfin/php-valigator

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

fishfin/php-valigator
=====================

Stand-alone PHP Class for Data Sanitization and Validation

1.0.4(5y ago)12261MITPHPPHP 5.5.\* || 5.6.\* || 7.\*

Since May 14Pushed 5y ago1 watchersCompare

[ Source](https://github.com/fishfin/php-valigator)[ Packagist](https://packagist.org/packages/fishfin/php-valigator)[ Docs](https://github.com/fishfin/php-valigator)[ RSS](/packages/fishfin-php-valigator/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (2)DependenciesVersions (4)Used By (0)

Valigator: Stand-alone PHP Class for Data Sanitization and Validation
---------------------------------------------------------------------

[](#valigator-stand-alone-php-class-for-data-sanitization-and-validation)

Lovers of minimalism, rejoice! Valigator is a stand-alone PHP class for data sanitization and validation. It has no library dependencies, implements programmer-friendly filter syntax, and is highly flexible. Its just one single class file, include it and you are already on the move!

### Valigator, huh?

[](#valigator-huh)

PHP API frameworks are picking up, fast. They are wonderfully minimalist, speedy, and vastly preferred over powerful yet sometimes clunky larger frameworks. To implement validations in API frameworks and projects, large vendor sources have to be installed, adding unnecessary additions to code-base and complexity. Valigator was created to address specifically that. And there's nothing that stops you from using Valigator in non-API projects. Go ahead, you'll love it!

##### Valigator Checklist:

[](#valigator-checklist)

```
  ✓ PHP (5.5.*, 5.6.*, 7.*)
  ✓ Stand-alone
  ✓ Data Sanitization
  ✓ Data Validation
  ✓ Simple
  ✓ Flexible
  ✓ Programmer-friendly

```

PS: Slim Framework 3 is awesome!

### Yet Another Vali\[dg\]ator

[](#yet-another-validgator)

Maybe. Maybe not. Yeah, Valigator draws inspiration from some of the good, nay, great ones. And adds its own good bits too. Just to get you interested: Filter Aliasing, Multiple Arguments, custom Labels, custom Validation Error Messages and more.

Anatomy of Valigator
--------------------

[](#anatomy-of-valigator)

### Terminology

[](#terminology)

- **field**
    The name of the data to which filters are mapped. Typically variable names in a POST request. A field may be mapped to no, one or multiple sanitization filters. A field may be mapped to no, one or multiple validation filters. Case-sensitive. For example, `loginId` is not the same as `loginid`.
- **value**
    The value of the field on which the filters run. Typically mapped to variable names in a POST request. Case-sensitive unless made case-insensitive by filters running on it. For example, `email` filter doesn't care if the value passed has upper or lower case characters.
- **filter**
    What some know as *rules*, Valigator prefers to call them *filters*. Because there are sanitization filters and validation filters, simple. Case-insensitive. For example, mis-typing `required` as `Required` makes no difference.
- **args**
    Arguments passed to filters. You may pass no, one or multiple arguments to a filter. Case-insensitive, unless made case-sensitive by filters requiring it. For example, `startswith` filter can validate if a field value starts with characters passed as a case-sensitive argument.
- **sanitization**
    Sanitization filters are known as simply 'sanitizations'. Sanitizations never error out so never emit any error messages.
- **validation**
    And vanitization filters are known as simply 'validations'. A validation can either pass or fail. If it fails, it emits one error message.
- **label**
    Human-readable label of the field which the programmer can set. If not set, labels default to upper-cased words of the field variable-name. For example, field `loginId` by default will be labelled `Login Id` (rad, isn't it!), but can be renamed by programmer to `Registered Login ID`. Variable-names of following patterns are automatically detected: *snake\_case*, *camelCase*, *PascalCase* and *train-case*. Some default label examples:
- `token` becomes `Token`
- `project_title` becomes `Project Title`
- `book-1` becomes `Book 1`
- `debitCardNumber` becomes `Debit Card Number`
- `FAQAnswer6` becomes `FAQ Answer 6`
- `SuspenseAccount#` becomes `Suspense Account #`
- **errormsg**
    Error messages emitted by validations (one per validation). Can be overwritten with custom error messages by the programmer per field per validation. Error messages may contain some special tags which will be replaced dynamically:
- `{field}` or `{label}` is replaced with label of the field
- `{fieldlineage}` or `{labellineage}` is replaced with label of the full field hierarchy in reverse order (as in `Node 3 of Node 2 of Node 1`
- `{fieldlineagef}` or `{labellineagef}` is replaced with label of the full field hierarchy in forward order (as in `Node 1.Node 2.Node 3`
- `{value}` is replaced with value of the field
- `{filter}` is replaced with name of the filter
- `{args}` or `{parms}` is replaced with delimited string of concatenated arguments to the filter
- `{arg1}`, ..., `{argn}` or `{parm1}`, ..., `{parmn}` are replaced with individual arguments to the filter if they exist (note that there is no {arg0} or {parm0})

### The Gears and Wheels

[](#the-gears-and-wheels)

Its easier to proceed from here with an example. Lets say we want to validate the following fields:

- loginId: required and must be an email ID
- name: required and must be a name
- creditCardNumber: not mandatory, but if provided must be a valid credit card number
- billAddressPINCode: not mandatory, but if provided must be a 6-digit number
- shipAddressPINCode: not mandatory, but if provided must be a 6-digit number

Yes, you noticed it, camelCase is just my preference.

Now lets say we are receiving the following data in an array (if you are not receiving the data in an array, you will need to create an array):

```
