Download the PHP package hollodotme/fluid-validator without Composer
On this page you can find all versions of the php package hollodotme/fluid-validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hollodotme/fluid-validator
More information about hollodotme/fluid-validator
Files in hollodotme/fluid-validator
Package fluid-validator
Short Description Validating values with a fluent interfaced class
License MIT
Informations about the package fluid-validator
FluidValidator
Validating data with a fluent interfaced class
Read more about this approach on my blog post.
Requirements
- PHP >= 5.5
- intl extension
Installation
Available validation methods
Conditional methods
Alvailable since version 1.1.0
:
Available since version 1.3.0
:
Non-validation methods
Available validation modes
Available message collectors
ScalarListMessageCollector
for collecting scalar message values (default, if none is provided)GroupedListMessageCollector
for collecting scalar key / scalar value messages grouped by key
Basic usage
Prints:
Conditional skip of checks
Available since version 1.1.0
.
You often want to check a value only if a previous condition is true.
Therefore the generic checkIf
method was added, alongside with if-methods for each check method.
Example:
Prints:
Usage with data provider
If you have an object covering a data structure like an array or something like that, e.g. a request object,
you can tell the FluidValidator
to use this object to retrieve the values to validate from it.
All you need to do is to implement the hollodotme\FluidValidator\Interfaces\ProvidesValuesToValidate
interface' method getValueToValidate( $key )
.
Usage with message collectors
Available since version 1.2.0
.
ScalarListMessageCollector (default)
Prints:
Note: Behaviour is the same as in the basic usage example above.
GroupedListMessageCollector
This collector expects messages to be an assoc. array with scalar keys and values.
Prints:
Note:
- The "list" key is given twice, so 2 messages were grouped under this key.
- The value for a group key is always an array with numeric keys
Custom message collector implementations
For a custom implementation of a message collector, simply create a class that implements
the hollodotme\FluidValidator\Interfaces\CollectsMessages
interface.
Extending FluidValidator with own checks
Internally all validation and conditional methods are mapped to protected check methods returning a boolean.
TRUE
if the check has passed, FALSE
otherwise.
So extending FluidValidator is very simple.
1. Extend the class and add a check method
Let's add a method that checks for a valid postal address.
2. Use it for validation / conditions
Now you're able to use this as a validation method with an additional message parameter and as a conditional method with an additional continue parameter:
3. Add method signatures
To have better auto completion in your IDE, add the relevant method signatures to the head phpdoc of your class.
All versions of fluid-validator with dependencies
ext-intl Version *