This is a simple validation engine written in PHP OOP for fun. If you need a robust PHP validator I recommend this one.
Install the package using Composer:
$ composer require pbrus/validation-engine=dev-master
Then go to the validation-engine/
directory and make dump-autoload
:
$ cd vendor/pbrus/validation-engine/
$ composer dump-autoload
If don't know what Composer is, see the simplified PHP installation.
Define a validator and set its labels (here username
and id
):
$validator = new ValidationEngine();
$validator->setLabels(array(
'username',
'id'
));
In the next step set constraints for each label:
$validator->setConstraints('username', array(
new NotEmptyValidator(array(
'notEmptyMessage' => 'Field username must be filled out'
)),
new LengthValidator(array(
'minLength' => 3,
'maxLength' => 25,
'lengthMessage' => "Field username must contain 3-25 letters"
))
));
$validator->setConstraints('id', array(
new NotEmptyValidator(array(
'notEmptyMessage' => "You must type your ID"
)),
new LengthValidator(array(
'minLength' => 10,
'maxLength' => 10,
'lengthMessage' => "Field ID must consist of 10 integers"
))
));
Then we can validate data:
if (!$validator->setFields(array(
'username' => 'John', // not validated: 'Jo'
'id' => '9876543210' // not validated: '123'
))
) {
echo $validator->getErrorMessage();
}
If you encounter any problems, please see the demo file index.php
.
The following examples of classes are defined:
- NotEmptyValidator
- LengthValidator
Feel free to add own classes.
Validation-engine is licensed under the MIT license.