Download the PHP package iulyanp/flex-validator without Composer
On this page you can find all versions of the php package iulyanp/flex-validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download iulyanp/flex-validator
More information about iulyanp/flex-validator
Files in iulyanp/flex-validator
Package flex-validator
Short Description A small validator based on Respect Validation library
License MIT
Informations about the package flex-validator
Flex Validator
A simple validator based on Respect Validation library, that provides an easy way to customize the error messages.
Installation
If you have composer installed globally you can run:
or you can go with:
Usage
FlexValidator can validate simple input:
For our little example the getErrors()
will return something like this:
But you can also validate arrays like this:
`
The getErrors
method will return an array with all breaking validation rules:
Validation methods
Currently you can validate simple values and multidimensional arrays.
The validate
method is a shortcut also for the array
method which will validate multidimensional arrays.
Disable named rules
As we saw in above the examples the error messages had the key of the errors array as the name of the broken rule:
alnum
, length
.
We can disable this with a simple call of the disableRulesName()
method on the FlexValidator.
Use dot error keys
Also the errors array can be returned with dot keys.
You can also use the dot keys
with disabled rule names
together.
Error groups
Sometime when we validate an array of data maybe we want to wrap the error messages in a group. With FlexValidator we can do this in two ways: global group and specific group.
Specific group
First let's take a look on how we can specify a specific group on a specific array key.
As you can see the field username
was wrapped in a group login
.
Global group
The global group will be applied only on the fields that don't have a specific group set. To specify a global group
we can pass the third argument to the validate
method.
Custom error messages
Almost all the time when we use a validation library we want to overwrite the default error messages. In the next steps we can see how FlexValidator made this objective very clean and easy to use.
Overwrite default error messages from RespectValidation
We can set the default error messages passing an array with the messages for each respect validation rule.
Custom error messages globally for an entire set of data
We can go further and use custom error messages globally when we validate a set of data by passing the array with
error messages as the fourth argument to the validate
method.
Custom error messages on specific value of an array
If we want to use a custom error message only for a specific field we can do so by specifying the messages
key as
an array with all the custom error messages. Here is an example.
As you can see the error message from the name
field is taken from the fourth argument of the validate
method and
the error messages for the phone
number are taken from the messages
array.