Download the PHP package phpixie/validate without Composer
On this page you can find all versions of the php package phpixie/validate. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download phpixie/validate
More information about phpixie/validate
Files in phpixie/validate
Package validate
Short Description Validation library for PHPixie
License BSD-3-Clause
Homepage http://phpixie.com
Informations about the package validate
Validate
PHPixie Validation library
Most PHP validation libraries share a large flaw - they only work with flat arrays and are mostly targeting form validation. This approach is irredeemably outdated, as when developing APIs and RESTful services we frequently have to work with request documents with complex structures. PHPixie Validate is designed to provide an easy approach to validating those. It requires no dependencies, so even if you are not using PHPixie it can still come in handy in your project.
Let's start with an easy flat array example first:
The full list of available filters can be found here. Every public method in these Registry classes is an available filter
You can also build the validator using an shorthand callback syntax:
Now let's try validating:
Working with results
As you can see above a Result object contains the errors appended to it and also
results of all fields. It may see that errors are just strings, while in fact they
are also classes that implement the magic __toString()
method for debugging convenience.
When working with forms you'll probably want to use your own error messages instead.
To do this get the type and parameters information from the error class and format
it accordingly, e.g. :
This way with a simple helper class you can customize all errors for your users.
Data structures
Now let's try a structured example:
It looks much better with the alternative syntax, since it follows the structure of your data:
Now let's try using it:
Try it out
To fire up this demo, just run: