19/11
2017
Wallogit.com
2017 © Pedro PelĂĄez
This is a simple validation engine written in PHP OOP for fun. If you need a robust PHP validator I recommend this one., (*2)
Install the package using Composer:, (*3)
$ composer require pbrus/validation-engine=dev-master
Then go to the validation-engine/ directory and make dump-autoload:, (*4)
$ cd vendor/pbrus/validation-engine/ $ composer dump-autoload
If don't know what Composer is, see the simplified PHP installation., (*5)
Define a validator and set its labels (here username and id):, (*6)
$validator = new ValidationEngine();
$validator->setLabels(array(
'username',
'id'
));
In the next step set constraints for each label:, (*7)
$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:, (*8)
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., (*9)
The following examples of classes are defined:, (*10)
Feel free to add own classes., (*11)
Validation-engine is licensed under the MIT license., (*12)